Skip to content

Use key as main identity for vega charts with selections#13559

Merged
lukasmasuch merged 4 commits intodevelopfrom
feature/key-as-main-identity-for-vega-chart-selections
Jan 13, 2026
Merged

Use key as main identity for vega charts with selections#13559
lukasmasuch merged 4 commits intodevelopfrom
feature/key-as-main-identity-for-vega-chart-selections

Conversation

@lukasmasuch
Copy link
Copy Markdown
Collaborator

Describe your changes

If a custom key is passed to a st.altair_chart and st.vega_lite_chart with 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

  • Added unit and e2e tests.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Jan 11, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 11, 2026

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13559/streamlit-1.52.2-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13559.streamlit.app (☁️ Deploy here if not accessible)

@lukasmasuch lukasmasuch added security-assessment-completed change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Jan 11, 2026
@lukasmasuch lukasmasuch marked this pull request as ready for review January 11, 2026 20:14
Copilot AI review requested due to automatic review settings January 11, 2026 20:14
@lukasmasuch
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_identity parameter from False to {"selection_mode"} for vega charts with selections, allowing the key to be the primary identifier while whitelisting selection_mode as 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(

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no bugs!

@github-actions
Copy link
Copy Markdown
Contributor

📉 Frontend coverage change detected

The frontend unit test (vitest) coverage has decreased by 0.0400%

  • Current PR: 86.5500% (12828 lines, 1725 missed)
  • Latest develop: 86.5900% (12828 lines, 1719 missed)

💡 Consider adding more unit tests to maintain or improve coverage.

📊 View detailed coverage comparison

@lukasmasuch lukasmasuch merged commit f7831d3 into develop Jan 13, 2026
44 checks passed
@lukasmasuch lukasmasuch deleted the feature/key-as-main-identity-for-vega-chart-selections branch January 13, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants