Use key as main identity for st.number_input#12437
Conversation
🎉 Snyk checks have passed. No issues have been found so far.✅ security/snyk check is complete. No issues have been found. (View Details) ✅ license/snyk check is complete. No issues have been found. (View Details) |
✅ PR preview is ready!
|
There was a problem hiding this comment.
Pull Request Overview
This PR enables st.number_input widgets to use a custom key as the main identity when provided, allowing dynamic parameter changes between reruns without recreating the widget in the frontend. This addresses issue #11277 by maintaining widget state when non-core parameters are modified.
- Modified the
key_as_main_identityparameter to whitelist specific parameters (min_value,max_value,step) that still affect widget identity - Added comprehensive unit tests to verify stable widget IDs with keys and parameter changes
- Implemented end-to-end tests demonstrating dynamic property updates while preserving widget state
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/streamlit/elements/widgets/number_input.py |
Updated key_as_main_identity to use a whitelist of core parameters instead of False |
lib/tests/streamlit/elements/number_input_test.py |
Added unit tests for stable ID behavior and whitelisted parameter validation |
e2e_playwright/st_number_input.py |
Added test script with dynamic number input demonstrating property updates |
e2e_playwright/st_number_input_test.py |
Added E2E test validating dynamic property changes preserve widget state |
| expect_markdown(app, "Initial number input value: 7") | ||
|
|
||
| # Click the toggle to update the number input props | ||
| from e2e_playwright.shared.app_utils import click_toggle |
There was a problem hiding this comment.
Move the import statement from e2e_playwright.shared.app_utils import click_toggle to the module level at the top of the file. According to the Python Guide, imports should be organized at the top of the file rather than inside functions to follow proper Python import organization.
Spotted by Diamond (based on custom rule: Python Guide)
Is this helpful? React 👍 or 👎 to let us know.
Describe your changes
If a custom
keyis passed to any of thest.number_inputwidgets it will be used as the main identity. This allows for dynamically changing other parameters in between reruns without causing the widgets to be recreated in the frontend.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.