π Β Β Mandatory Date Range Picker
Submitted by Zachary Blackwood
Summary
Just like st.date_input, but enforces that it always and only returns a start and end date, even if the user has only selected one of the dates. Until the user selects both dates, the app will not run.
Functions
date_range_picker
Working with date_input with a date range is frustrating becuase if you're assuming you will get a start and end date out of it, your code can break (not to mention your type hints), because if a user clicks on just one date, the app will go ahead and run with a single output. This widget enforces a start and end date being selected, and will stop the app if only one is chosen.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the date widget |
required |
default_start |
Optional[date]
|
Default start date. Defaults to None. |
None
|
default_end |
Optional[date]
|
Default end date. Defaults to None. |
None
|
min_date |
Optional[date]
|
Minimum date. Defaults to None. |
None
|
max_date |
Optional[date]
|
Maximum date. Defaults to None. |
None
|
error_message |
str
|
Error message when only one date is chosen. Defaults to "Please select start and end date". |
'Please select start and end date'
|
key |
Optional[str]
|
Widget key. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tuple[date, date]
|
Tuple[date, date]: Start and end date chosen in the widget |
Source code in src/streamlit_extras/mandatory_date_range/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example
def example():
st.write(
"""
This is an example of a date range picker that *always* returns a start and
end date, even if the user has only selected one of the dates. Until the
user selects both dates, the app will not run.
"""
)
result = date_range_picker("Select a date range")
st.write("Result:", result)