Allow dynamic changes to st.radio options when key is provided#13611
Allow dynamic changes to st.radio options when key is provided#13611lukasmasuch merged 9 commits intodevelopfrom
st.radio options when key is provided#13611Conversation
✅ 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 enables dynamic option changes for st.radio when a key is provided by migrating from index-based to string-based widget values. The change aligns with the approach previously implemented for st.selectbox and st.multiselect, allowing options to change without triggering widget identity changes while gracefully handling invalid values.
Changes:
- Migrated
st.radiofrom index-based to string-based widget values (formatted options) - Modified widget identity computation to use
key_as_main_identity=Trueinstead of whitelisting specific parameters - Updated validation logic to reset to default when selected value is removed from options
Reviewed changes
Copilot reviewed 8 out of 20 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
proto/streamlit/proto/Radio.proto |
Deprecated value field and added raw_value field for string-based values |
lib/streamlit/elements/widgets/radio.py |
Refactored RadioSerde to use string-based serialization/deserialization with formatted options mapping |
lib/streamlit/testing/v1/element_tree.py |
Updated AppTest to send string values instead of int values for radio widgets |
frontend/lib/src/components/widgets/Radio/Radio.tsx |
Changed widget to use string values and convert to/from index for UIRadio component |
frontend/lib/src/components/widgets/Radio/Radio.test.tsx |
Updated all tests to expect setStringValue calls instead of setIntValue |
lib/tests/streamlit/elements/radio_test.py |
Added comprehensive tests for dynamic options behavior and removed obsolete whitelisting tests |
e2e_playwright/st_radio.py |
Updated test app to demonstrate dynamic options with value preservation and reset scenarios |
e2e_playwright/st_radio_test.py |
Enhanced e2e test to verify both value preservation and reset behaviors |
SummaryThis PR enables dynamic option changes for
Code QualityPython Backend (
|
| option_index = index_(self.options, v) | ||
| return self.formatted_options[option_index] | ||
| except ValueError: | ||
| # Value is a string that's not in options (shouldn't happen for radio) |
There was a problem hiding this comment.
[question] Does this happen sometimes when the format string changes?
There was a problem hiding this comment.
I've refactored this code to fix an issue with custom objects without eq (similar to PR #13648). The old code path that caught ValueError from index_() no longer exists.
There was a problem hiding this comment.
Does it make sense to add a test with the format string changing?
sfc-gh-lwilby
left a comment
There was a problem hiding this comment.
LGTM -- just some questions about the format string handling.
…ain-identity-for-radio-options
📈 Frontend coverage change detectedThe frontend unit test (vitest) coverage has increased by 0.0600%
🎉 Great job on improving test coverage! |
|
@cursor review |
SummaryThis PR switches Code Quality
Test Coverage
Backwards Compatibility
Security & RiskNo security issues found. Main risk is a UX regression where selection appears cleared after Recommendations
VerdictCHANGES REQUESTED: The format_func-change path can desynchronize UI selection from server state and should be addressed before merge. This is an automated AI review using |
Describe your changes
Allow dynamically changing the options for
st.radiowithout 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.This also applies a needed change from index-based widget value to string-based (formatted options). The same change was already implemented for
st.selectboxandst.multiselectsome time ago.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.