Skip to content

[SC-15877] Support pandas Styler outputs in test summary tables#497

Open
cachafla wants to merge 1 commit intomainfrom
cachafla/sc-15877/style-summary-table-cells
Open

[SC-15877] Support pandas Styler outputs in test summary tables#497
cachafla wants to merge 1 commit intomainfrom
cachafla/sc-15877/style-summary-table-cells

Conversation

@cachafla
Copy link
Copy Markdown
Contributor

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.Styler from 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

{ value, bgcolor, color, fontWeight, textAlign }

What changed

  • validmind/tests/load.pypandas.io.formats.style.Styler is now an accepted TABLE_TYPES output from a test.
  • validmind/tests/output.pyTableOutputHandler accepts Styler and converts it via a new _convert_styler helper. 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 under value. Unstyled cells stay as plain scalars.
  • validmind/vm_models/html_renderer.pyStatefulHTMLRenderer renders 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

  1. Open the new how-to: notebooks/how_to/tests/custom_tests/style_custom_test_tables.ipynb.
  2. Run a custom test that returns a pandas.Styler (e.g., df.style.map(lambda v: "background-color: #EAF4FF" if ...)).
  3. Inspect the resulting TestResult — styled cells should be dicts of the shape above; unstyled cells remain scalars.
  4. Render the result in a notebook — the styled cells should display with the matching colors / weights, not as {'value': ...} JSON.
  5. Run the test suite: pytest tests/test_results.py -k styler.

What needs special review?

  • The _convert_styler mapping from CSS properties to schema keys (style_key_map) — this defines what styling is portable. We currently support background[-color], color, font-weight, text-align. Anything else is dropped.
  • Formatter handling: the display value is run through the Styler's _display_funcs before being placed in the cell dict, so user-applied number formatting is preserved.

Dependencies, breaking changes, and deployment notes

Release notes

Tests can now return a pandas.Styler to 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 new style_custom_test_tables how-to notebook.

Checklist

  • What and why
  • Screenshots or videos (Frontend) — N/A
  • How to test
  • What needs special review
  • Dependencies, breaking changes, and deployment notes
  • Labels applied
  • PR linked to Shortcut
  • Unit tests added
  • Tested locally
  • Documentation updated (how-to notebook added)
  • Environment variable additions/changes documented (if required)

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>
@cachafla cachafla added enhancement New feature or request python Pull requests that update Python code labels Apr 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

PR Summary

This PR introduces significant improvements to the handling and rendering of custom test tables within the ValidMind framework. The key changes include:

  1. A new Jupyter notebook (style_custom_test_tables.ipynb) has been added that demonstrates how to use native pandas Styler to apply rich, portable cell-level styling to test result tables. Two custom tests are showcased: one returning a pandas Styler and the other returning structured cell dictionaries. This helps users choose the appropriate method based on their needs.

  2. The unit tests in tests/test_results.py have been updated to verify that the table output handler correctly converts pandas Styler objects by preserving CSS properties (e.g., background-color, color, font-weight, text-align) into a structured format. Additional tests ensure that interactive figure toggling behaves correctly under various environment variable values.

  3. Updates in the ValidMind core include modifications in the table conversion logic to support pandas Styler alongside DataFrame and ResultTable objects. In particular, the TableOutputHandler has been extended with a helper function that converts a Styler instance into a DataFrame with portable cell styles.

  4. The HTML renderer has been enhanced to support rendering of structured cell dictionaries. A new helper function ensures that cell values are safely rendered as HTML after applying necessary style mappings and escaping.

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

  • Verify that the conversion of a pandas Styler object to a structured DataFrame preserves all intended CSS properties and formats.
  • Test the HTML rendering of structured cell dictionaries, checking that all style attributes are correctly applied and escaped.
  • Run the interactive figure tests with different environment settings to verify that plotly data toggling behaves as expected.
  • Simulate edge cases by providing empty or malformed table data to ensure robust error handling in the TableOutputHandler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant