Use key as main identity for vega charts with selections#13559
Use key as main identity for vega charts with selections#13559lukasmasuch merged 4 commits intodevelopfrom
key as main identity for vega charts with selections#13559Conversation
✅ 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!
|
|
@cursor review |
There was a problem hiding this comment.
Pull request overview
This pull request implements a feature that uses the key parameter as the main identity for st.altair_chart and st.vega_lite_chart when selections are activated. This allows users to dynamically change chart data and other parameters between reruns without recreating the widget in the frontend and losing selection state.
Changes:
- Modified
key_as_main_identityparameter fromFalseto{"selection_mode"}for vega charts with selections, allowing the key to be the primary identifier while whitelistingselection_modeas a parameter that can invalidate the cached state - Added comprehensive unit tests covering ID stability with keys, ID changes when selection_mode changes, and ID changes without keys
- Added E2E test demonstrating that selections persist when chart data is updated but the key remains constant
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/streamlit/elements/vega_charts.py |
Changed key_as_main_identity from False to {"selection_mode"} for vega lite charts with selections, making the key the main identity while whitelisting selection_mode as a parameter that can change the ID |
lib/tests/streamlit/elements/vega_charts_test.py |
Added comprehensive unit tests for ID stability with keys, verifying stable IDs when data changes but key/selection_mode stay constant, and verifying ID changes when selection_mode changes or no key is provided |
e2e_playwright/st_altair_chart_basic_select.py |
Added test app code demonstrating selection persistence across data updates using a chart with a fixed key |
e2e_playwright/st_altair_chart_basic_select_test.py |
Added E2E test verifying that selections persist when chart data is updated but the key remains the same |
Comments suppressed due to low confidence (1)
e2e_playwright/st_altair_chart_basic_select.py:290
- This assignment to 'selection' is unnecessary as it is redefined before this value is used.
selection = st.altair_chart(
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0400%
💡 Consider adding more unit tests to maintain or improve coverage. |
Describe your changes
If a custom
keyis passed to ast.altair_chartandst.vega_lite_chartwith selections activated, it will be used as the main identity. This allows for dynamically changing other parameters between reruns without recreating the widgets in the frontend and losing their state.GitHub Issue Link (if applicable)
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.