Skip to content

Allow dynamically changing min_value and max_value in st.datetime_input when key is provided#13620

Merged
lukasmasuch merged 3 commits intodevelopfrom
lukasmasuch/datetime-input-minmax
Jan 20, 2026
Merged

Allow dynamically changing min_value and max_value in st.datetime_input when key is provided#13620
lukasmasuch merged 3 commits intodevelopfrom
lukasmasuch/datetime-input-minmax

Conversation

@lukasmasuch
Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch commented Jan 16, 2026

Describe your changes

Allow dynamically changing the min_value, max_value of st.datetime_input without 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

  • Added e2e and unit tests.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

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]>
Copilot AI review requested due to automatic review settings January 16, 2026 22:45
@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Jan 16, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 16, 2026

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13620/streamlit-1.53.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13620.streamlit.app (☁️ Deploy here if not accessible)

Copy link
Copy Markdown
Contributor

Copilot AI left a 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 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_identity to 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

@lukasmasuch lukasmasuch added security-assessment-completed change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Jan 16, 2026
@lukasmasuch lukasmasuch changed the title Allow dynamically changing min and max in st.datetime_input Allow dynamically changing min_value and max_value in st.datetime_input when key is provided Jan 16, 2026
@lukasmasuch
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a 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!

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.
"""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also test changing the value to something valid within the range from the default and ensuring that is preserved?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 👍

@lukasmasuch lukasmasuch merged commit e5dadc8 into develop Jan 20, 2026
44 checks passed
@lukasmasuch lukasmasuch deleted the lukasmasuch/datetime-input-minmax branch January 20, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants