3 3 Date Input Explained
Key Concepts
- st.date_input: A widget that allows users to input a date.
- Date Format: The format in which the date is displayed and accepted.
- Default Date: The initial date displayed when the widget is loaded.
- Date Range: The range of dates that can be selected.
st.date_input
st.date_input
is a Streamlit widget that provides a calendar-like interface for users to select a date. This widget is particularly useful in applications where date selection is required, such as scheduling, booking, or filtering data by date.
Date Format
The date input widget typically follows the ISO 8601 format (YYYY-MM-DD). This format is widely accepted and ensures consistency across different systems. Users can select a date from the calendar, and the selected date will be displayed in the specified format.
Default Date
The default date is the initial date displayed when the widget is loaded. This can be set using the value
parameter. If no default date is specified, the current date will be used.
Date Range
The date range specifies the minimum and maximum dates that can be selected. This is useful for restricting the selection to a specific period. The range can be set using the min_value
and max_value
parameters.
Examples
Example 1: Basic Date Input
import streamlit as st selected_date = st.date_input("Select a date") st.write(f"You selected: {selected_date}")
Example 2: Setting a Default Date
import streamlit as st from datetime import date default_date = date(2023, 10, 1) selected_date = st.date_input("Select a date", value=default_date) st.write(f"You selected: {selected_date}")
Example 3: Restricting Date Range
import streamlit as st from datetime import date min_date = date(2023, 1, 1) max_date = date(2023, 12, 31) selected_date = st.date_input("Select a date", min_value=min_date, max_value=max_date) st.write(f"You selected: {selected_date}")
Analogies
Think of st.date_input
as a digital calendar where users can pick a specific day. The default date is like the current day highlighted when you open the calendar. The date range is like setting boundaries on the calendar, ensuring users can only select dates within a specific period.
By mastering st.date_input
, you can enhance your Streamlit applications by providing users with a convenient and intuitive way to select dates, making your apps more interactive and user-friendly.