Allow dynamically changing min_value and max_value in st.datetime_input when key is provided#13620
Conversation
Enable dynamic min_value and max_value changes for datetime_input when a key is provided, following the same pattern as st.date_input (PR #13549). Changes: - Updated key_as_main_identity to exclude min/max from element ID stability - Added _validate_datetime_value() to handle bound validation and value resets - Added logic to track explicit bounds and reset values when out of bounds - Added unit tests for dynamic min/max scenarios - Updated E2E tests to validate dynamic bounds behavior Co-Authored-By: Claude <[email protected]>
✅ 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 min_value and max_value changes for st.datetime_input when a key is provided, mirroring the implementation from PR #13549 for st.date_input. When bounds change and the current value falls outside the new bounds, the widget resets to the default value.
Changes:
- Added
_validate_datetime_value()function to validate datetime values against dynamic bounds - Modified
key_as_main_identityto exclude min/max parameters, enabling stable widget IDs when bounds change - Added validation logic that resets values when they fall outside explicitly set bounds
- Added comprehensive unit tests and E2E tests for dynamic bounds behavior
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/streamlit/elements/widgets/time_widgets.py | Implemented _validate_datetime_value() function and integrated validation logic into _datetime_input() to support dynamic min/max bounds |
| lib/tests/streamlit/elements/datetime_input_test.py | Added three unit tests covering dynamic min, max, and value preservation scenarios |
| e2e_playwright/st_datetime_input.py | Updated test app to include min_value and max_value parameters in the dynamic test case |
| e2e_playwright/st_datetime_input_test.py | Enhanced E2E test to validate dynamic bounds behavior including value reset and anti-regression check |
min_value and max_value in st.datetime_input when key is provided
|
@cursor review |
| A tuple of (validated_value, was_reset) where validated_value is either the | ||
| original value (if valid) or the default value (if reset was needed), and | ||
| was_reset indicates whether a reset occurred. | ||
| """ |
There was a problem hiding this comment.
I think this logic can be simplified
if current_value is None or not has_explicit_bounds:
return current_value, False
if current_value < parsed_values.min or current_value > parsed_values.max:
return parsed_values.value, True
return current_value, False
There was a problem hiding this comment.
Done! Simplified the logic using early returns as suggested.
| # AppTest requires an additional run to process the widget with the new bounds | ||
| at = at.run() | ||
| # Value should be preserved because it's still within the new bounds | ||
| assert at.datetime_input[0].value == datetime(2024, 5, 15, 12, 0) |
There was a problem hiding this comment.
Maybe also test changing the value to something valid within the range from the default and ensuring that is preserved?
Describe your changes
Allow dynamically changing the
min_value,max_valueofst.datetime_inputwithout triggering an identity change / state reset. If the current selected value isn't in the range, it will be reset to the default value.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.