Support resolving to theme colors configs for charts#13739
Support resolving to theme colors configs for charts#13739mayagbarnes merged 7 commits intodevelopfrom
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
There was a problem hiding this comment.
Pull request overview
This PR adds support for built-in Streamlit theme color names (red, orange, yellow, green, blue, violet, gray/grey, primary) in the color parameter of chart functions. Colors are passed through from the backend as strings and resolved to theme color values on the frontend, enabling automatic adaptation to light/dark themes.
Changes:
- Added built-in color name validation and handling in Python backend (
color_util.py,built_in_chart_utils.py) - Implemented frontend color resolution logic in TypeScript (
colorUtils.ts,useVegaElementPreprocessor.ts) - Updated docstrings for all chart functions to document the new color name options
- Added comprehensive unit tests (Python and TypeScript) and E2E tests with custom theme verification
Reviewed changes
Copilot reviewed 11 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
lib/streamlit/elements/lib/color_util.py |
Added BUILTIN_COLOR_NAMES constant and is_builtin_color_name() function for validating built-in color names |
lib/streamlit/elements/lib/built_in_chart_utils.py |
Modified _get_color_encoding() to pass through built-in color names as-is instead of converting them to CSS colors |
lib/streamlit/elements/vega_charts.py |
Updated docstrings for line_chart, area_chart, bar_chart, and scatter_chart to document built-in color name support |
lib/tests/streamlit/elements/lib/color_util_test.py |
Added unit tests for is_builtin_color_name() with positive and negative test cases |
lib/tests/streamlit/elements/vega_charts_test.py |
Added tests for built-in color names, mixed colors, and column name priority with negative assertions |
frontend/lib/src/components/elements/ArrowVegaLiteChart/colorUtils.ts |
New utility module with color name validation and resolution to theme values |
frontend/lib/src/components/elements/ArrowVegaLiteChart/colorUtils.test.ts |
Comprehensive unit tests covering all built-in color names, case-insensitivity, and invalid inputs |
frontend/lib/src/components/elements/ArrowVegaLiteChart/useVegaElementPreprocessor.ts |
Integrated resolveBuiltinColorsInSpec() into the chart preprocessing pipeline |
frontend/lib/src/components/elements/ArrowVegaLiteChart/useVegaElementPreprocessor.test.ts |
Added tests for color resolution in various chart structures (single values, ranges, layers) |
e2e_playwright/st_chart_builtin_colors.py |
Test app demonstrating all built-in color names across 4 chart types |
e2e_playwright/st_chart_builtin_colors_test.py |
E2E test with custom theme configuration to verify color resolution |
e2e_playwright/__snapshots__/ |
Snapshot images for visual regression testing (chromium and webkit) |
This stack of pull requests is managed by Graphite. Learn more about stacking. |
SummaryThis PR adds support for Streamlit's theme colors in chart Key changes:
Code QualityThe implementation is clean and well-structured:
Test CoverageTest coverage is comprehensive: Python Unit Tests:
Frontend Unit Tests:
E2E Tests:
Backwards CompatibilityThe implementation is fully backwards compatible:
The test file change updating Security & RiskNo security concerns identified. The changes are isolated to color value handling and don't introduce any new attack vectors. Low regression risk:
RecommendationsNo blocking issues found. Minor suggestions for future consideration (not required for this PR):
VerdictAPPROVED: Well-implemented feature with comprehensive test coverage (Python unit, TypeScript unit, and E2E), clean code that follows existing patterns, and proper backwards compatibility handling. The PR is ready for merge. This is an automated AI review using |
SummaryAdds support for built-in chart color names (red/orange/etc.) by passing them through the backend and resolving them to theme colors in the Vega-Lite preprocessor. Updates docs and adds unit, frontend, and e2e coverage plus new snapshots. Code QualityClear separation between backend validation/pass-through and frontend resolution, with a focused utility for spec mutation and targeted tests. The new logic is localized and avoids altering existing color pathways. Test CoverageGood coverage across layers: Python unit tests for validation and spec pass-through, frontend unit tests for resolution behavior, and an e2e snapshot that exercises all built-in names. The tests are aligned with the changes and cover positive and negative paths. Backwards CompatibilityBackwards compatible. This expands accepted color inputs to include built-in names without changing existing hex/tuple behavior or column-name precedence. Security & RiskNo security concerns identified. Primary risk is limited to edge-case Vega-Lite compositions that may not get color resolution if nested more deeply than the handled structures. Recommendations
VerdictAPPROVED: Solid implementation with comprehensive tests; only minor best-practice follow-ups suggested. This is an automated AI review using |
lukasmasuch
left a comment
There was a problem hiding this comment.
LGTM 👍 maybe check if we need/can to support more compositions.
| const COLOR_NAME_TO_THEME_KEY: Record<string, keyof EmotionTheme["colors"]> = { | ||
| red: "redColor", | ||
| orange: "orangeColor", | ||
| yellow: "yellowColor", | ||
| green: "greenColor", | ||
| blue: "blueColor", | ||
| violet: "violetColor", | ||
| gray: "grayColor", | ||
| grey: "grayColor", // alias | ||
| primary: "primary", | ||
| } |
There was a problem hiding this comment.
Potential follow-up. We already do something very similar in these column types:
- https://github.com/streamlit/streamlit/blob/develop/frontend/lib/src/components/widgets/DataFrame/columns/ChartColumn.ts
- https://github.com/streamlit/streamlit/blob/develop/frontend/lib/src/components/widgets/DataFrame/columns/ProgressColumn.ts
It might make sense to move some utils/mapping into some shared code somewhere.
There was a problem hiding this comment.
Good news I already have a PR in the works to refactor this here
📈 Frontend coverage change detectedThe frontend unit test (vitest) coverage has increased by 0.0900%
🎉 Great job on improving test coverage! |

Describe your changes
Adds support for Streamlit's theme colors in the
color=parameter of built-in chart functions (st.line_chart,st.bar_chart,st.area_chart,st.scatter_chart) and base chart functions (st.vega_lite_chart&st.altair_chart).Color names are resolved to theme configurations, enabling theme-aware color usage that automatically adapts to light/dark mode.
red=theme.redColororange=theme.orangeColoryellow=theme.yellowColorblue=theme.blueColorgreen=theme.greenColorviolet=theme.violetColorgray/grey=theme.grayColorprimary=theme.primaryColorNOTE: This applies to built-in and base chart functions because they share the same pre-processor.
Co-authored-by: K [email protected]
GitHub Issue Link
Closes #12694
Testing Plan