-
Notifications
You must be signed in to change notification settings - Fork 4k
Allow dynamic changes to multiselect options when key is provided
#13448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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!
|
…multiselect-options
key is provided
There was a problem hiding this 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 enables dynamic changes to st.multiselect options when a key is provided, maintaining widget identity and preserving valid selections while filtering out invalid ones when the options list changes.
Key changes:
- Removed
optionsandformat_funcfrom the identity computation when a key is provided, allowing these to change without resetting widget state - Added validation logic to filter multiselect values against current options, preserving valid selections and removing invalid ones
- Refactored selectbox validation logic into reusable utility functions
Reviewed changes
Copilot reviewed 7 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
lib/streamlit/elements/widgets/multiselect.py |
Modified identity computation to exclude options and format_func from whitelisted params; added validation logic to filter invalid selections when options change |
lib/streamlit/elements/widgets/selectbox.py |
Refactored validation logic to use new validate_value_against_options utility function |
lib/streamlit/elements/lib/options_selector_utils.py |
Added two new validation functions: validate_value_against_options for selectbox and validate_multiselect_value_against_options for multiselect |
lib/tests/streamlit/elements/multiselect_test.py |
Updated stable ID tests to reflect new whitelisted params; added three new AppTest tests covering option expansion, filtering, and complete removal scenarios |
lib/tests/streamlit/elements/lib/options_selector_utils_test.py |
Added comprehensive parameterized tests for both new validation utility functions |
e2e_playwright/st_multiselect_test.py |
Enhanced test_dynamic_multiselect_props to test both filtering and preservation behaviors with improved documentation |
e2e_playwright/st_multiselect.py |
Updated test app to change both options and format_func between initial and updated states; changed accept_new_options to False consistently to avoid identity changes |
|
@cursor review |
There was a problem hiding this 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!
SummaryThis PR enables dynamic changes to
The implementation introduces two new validation utility functions that are shared between Key Changes:
Code QualityStrengths:
Minor Observations:
Test CoverageUnit Tests:
E2E Tests:
Test Quality Notes:
Backwards Compatibility✅ This change is backwards compatible.
Migration Note: Users relying on the previous behavior where changing options would reset state might see different behavior now - selections that remain valid will be preserved. This is the intended fix for issues #7855 and #11277. Security & Risk✅ No security concerns identified.
Risk Assessment:
RecommendationsThe PR is well-implemented and ready for merge. A few minor suggestions for consideration:
VerdictAPPROVED: This PR provides a well-tested, clean implementation that fixes a real user pain point (#7855, #11277). The code follows existing patterns, has comprehensive test coverage, and is backwards compatible. The validation logic is correctly shared between selectbox and multiselect widgets. This is an automated AI review. Please verify the feedback and use your judgment. |
| ) | ||
|
|
||
|
|
||
| def validate_value_against_options( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking): Because these functions validate and also have a possible side-effect of changing state, consider either:
- Giving them either a name that makes the potential for side-effects clearer.
- Refactoring this into 2 different functions and have the widget layer compose the functions so that we can keep validation logic pure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed the function to make it a bit more explicit
📉 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
Allow dynamically changing the options for
st.multiselectwithout triggering an identity change / state reset. If the current selected options isn't in the list of available option, it will be reset to the default value.GitHub Issue Link (if applicable)
st.multiselectreturns empty on dynamically changing options. #7855Testing 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.