I have an application and it has a date input, for user friendly input i want it to have multiple options in the date input, i.e today, yesterday, custom range, last week . last month, last year, last quarter etc.
Refer the image, How can i achieve the same in streamlit?
It not as nice as your example, but here a begining of hint i would do :
from datetime import datetime, timedelta
# Function to get the date range for "Last Week", "Last Month", etc.
def get_date_range(option):
today = datetime.today()
if option == "Today":
start_date = today
end_date = today
elif option == "Yesterday":
start_date = today - timedelta(days=1)
end_date = today - timedelta(days=1)
elif option == "Last Week":
start_date = today - timedelta(weeks=1)
end_date = today - timedelta(days=1)
elif option == "Last Month":
start_date = today.replace(day=1) - timedelta(days=1)
start_date = start_date.replace(day=1)
end_date = today.replace(day=1) - timedelta(days=1)
elif option == "Last Year":
start_date = today.replace(year=today.year - 1, month=1, day=1)
end_date = today.replace(year=today.year - 1, month=12, day=31)
elif option == "Last Quarter":
if today.month <= 3:
start_date = today.replace(year=today.year - 1, month=10, day=1)
end_date = today.replace(year=today.year - 1, month=12, day=31)
elif today.month <= 6:
start_date = today.replace(month=1, day=1)
end_date = today.replace(month=3, day=31)
elif today.month <= 9:
start_date = today.replace(month=4, day=1)
end_date = today.replace(month=6, day=30)
else:
start_date = today.replace(month=7, day=1)
end_date = today.replace(month=9, day=30)
else:
start_date, end_date = None, None
return start_date, end_date
# Streamlit selectbox for date range options
option = st.selectbox("Choose a Date Range",
["Today", "Yesterday", "Last Week", "Last Month", "Last Year", "Last Quarter",
"Custom Range"])
# Get the start and end date based on the selected option
start_date, end_date = get_date_range(option)
start_date = st.date_input("Start date",value=(start_date, end_date))
Thank you, but this wont give me the custom and some other details anyways it would help me build the one.
Thanks again.