[SC-15877] Support pandas Styler outputs in test summary tables#497
[SC-15877] Support pandas Styler outputs in test summary tables#497
Conversation
Accept pandas Styler as a valid table output type. TableOutputHandler
now converts a Styler into a DataFrame whose styled cells are
serialized as {value, bgcolor, color, fontWeight, textAlign} dicts —
a portable, JSON-safe schema consumed by the documentation UI and
report renderers.
StatefulHTMLRenderer renders structured cells back to HTML for
in-notebook display so notebook output matches portal rendering
without leaking raw {'value': ...} JSON.
Adds a how-to notebook demonstrating styled-cell output from a
custom test, plus tests covering the Styler conversion path.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
PR SummaryThis PR introduces significant improvements to the handling and rendering of custom test tables within the ValidMind framework. The key changes include:
These changes aim to offer improved flexibility and consistency in how styled test tables are processed, displayed, and logged, leading to a better user experience when working with visual test outputs. Test Suggestions
|
What and why?
Lets test authors style cells in their test summary tables — color-coded status pills, severity shading, bold thresholds — by returning a
pandas.Stylerfrom a test. Styled cells are serialized into a portable, JSON-safe schema that the documentation UI, HTML reports, and DOCX exports all render consistently.This PR is the library piece. The backend (HTML/DOCX rendering) and frontend (UI rendering) pieces are in companion PRs.
Schema (shared across the three repos): cells may be a scalar or a dict of the form
What changed
validmind/tests/load.py—pandas.io.formats.style.Styleris now an acceptedTABLE_TYPESoutput from a test.validmind/tests/output.py—TableOutputHandleracceptsStylerand converts it via a new_convert_stylerhelper. Each cell that has CSS styles applied (background-color/background,color,font-weight,text-align) becomes a{value, bgcolor, color, fontWeight, textAlign}dict; the formatted display value (per the Styler's formatters) is preserved undervalue. Unstyled cells stay as plain scalars.validmind/vm_models/html_renderer.py—StatefulHTMLRendererrenders structured cells back to HTML for in-notebook display, so notebook output matches portal rendering instead of showing raw{'value': ...}JSON. CSS values are HTML-escaped.notebooks/how_to/tests/custom_tests/style_custom_test_tables.ipynb— a how-to demonstrating styled-cell output from a custom test.tests/test_results.py— covers the Styler conversion path (formatter preservation, all four CSS properties, HTML rendering).How to test
notebooks/how_to/tests/custom_tests/style_custom_test_tables.ipynb.pandas.Styler(e.g.,df.style.map(lambda v: "background-color: #EAF4FF" if ...)).TestResult— styled cells should be dicts of the shape above; unstyled cells remain scalars.{'value': ...}JSON.pytest tests/test_results.py -k styler.What needs special review?
_convert_stylermapping from CSS properties to schema keys (style_key_map) — this defines what styling is portable. We currently supportbackground[-color],color,font-weight,text-align. Anything else is dropped._display_funcsbefore being placed in the cell dict, so user-applied number formatting is preserved.Dependencies, breaking changes, and deployment notes
backend— https://github.com/validmind/backend/pull/3040frontend— https://github.com/validmind/frontend/pull/2470pd.DataFrame,dict, or scalar tables are unaffected.Release notes
Tests can now return a
pandas.Stylerto apply cell-level styling (background color, text color, bold, alignment) to summary tables — the styling is preserved consistently in the documentation UI, HTML reports, and DOCX exports. See the newstyle_custom_test_tableshow-to notebook.Checklist