-
Notifications
You must be signed in to change notification settings - Fork 4k
Allow dynamic changes to selectbox options when key is provided
#13383
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!
|
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 PR modifies the selectbox widget to allow dynamic option changes while preserving widget state when a key is provided. The key change is removing "options" from key_as_main_identity, making the widget ID stable across option changes, and adding validation logic to handle cases where the selected value is no longer in the updated options.
Key Changes
- Widget ID computation now excludes "options" parameter, maintaining stable IDs when options change dynamically with a key
- Added validation logic to detect and handle cases where the previously selected value is removed from options
- Updated tests to reflect the new behavior and added integration tests for dynamic option scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| lib/streamlit/elements/widgets/selectbox.py | Modified key_as_main_identity to only include accept_new_options, added validation logic to reset values when selected option is removed from new options list, and updated return value handling |
| lib/tests/streamlit/elements/selectbox_test.py | Added test for accept_new_options ID behavior, renamed and modified test to verify stable IDs with dynamic options/format_func, and added two integration tests for option expansion and shrinking scenarios |
|
@cursor review |
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0000%
✅ Coverage change is within normal range. |
SummaryThis PR changes Code Quality
Test Coverage
Backwards Compatibility
Security & Risk
Recommendations
VerdictCHANGES REQUESTED: The dynamic-options behavior looks good, but This is an automated AI review. Please verify the feedback and use your judgment. |
SummaryThis PR allows dynamically changing the
Code QualityStrengths:
Minor observations:
Test CoverageUnit Tests (lib/tests/streamlit/elements/selectbox_test.py):
E2E Tests (e2e_playwright/st_selectbox_test.py):
Test best practices compliance:
Note: Some unit tests require double Backwards CompatibilityThis change is backwards compatible. Previous behavior: Changing New behavior:
The only theoretical breaking change would be if someone intentionally relied on options changes to reset selection, which would be an unusual pattern. The new behavior is more intuitive and aligns with user expectations. Security & RiskNo security concerns identified:
Regression risk: Low
Recommendations
def test_enum_coercion_off():
"""With coercion disabled, the old class value is reset to new class default."""
selectbox = at.selectbox[0]
original_class = selectbox.value.__class__
at = selectbox.set_value(original_class.C).run()
# Value should reset to default (A) since old class C is not in new class options
assert at.selectbox[0].value.name == "A", "Value should reset to default with coercion off"
assert at.text[0].value == at.text[1].value, "Enum Class ID not the same"
assert at.text[2].value == "True", "Not all enums found in class"
These are minor suggestions and do not block approval. VerdictAPPROVED: This PR provides a well-implemented solution for dynamic selectbox options with comprehensive test coverage, proper backwards compatibility handling, and follows Streamlit code patterns. The changes are isolated, defensive, and address legitimate user needs documented in multiple issues. This is an automated AI review. Please verify the feedback and use your judgment. |
Describe your changes
Allow dynamically changing the options for
st.selectboxwithout 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.selectboxsometimes ignores user input when order ofoptionschanges #8496Testing 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.