How to work date input with shortcuts

I wanted to create a flow where users click on that shortcuts (today, this month, this week) the date will change automatically. please help.

Hi,

A common pattern for achieving this is with session state and widget callbacks. Here’s an example (using numeric input not dates, just because datetime is a pain :upside_down_face:). The start / end values are constrained, which is only possible via callbacks.

import streamlit as st

state = st.session_state

def _set_start_cb():
    if state.start > state.end:
        state.start = state.end
def _set_end_cb():
    if state.end < state.start:
        state.end = state.start

start = st.number_input('Start', value=0, on_change=_set_start_cb, key='start')
end = st.number_input('End', value=10, on_change=_set_end_cb, key='end')

def _make_same_cb():
    state.end = state.start
def _make_plus7_cb():
    state.end = state.start+7
def _make_times2_cb():
    state.end = state.start*2

st.button('Same', on_click=_make_same_cb)
st.button('Plus 7', on_click=_make_plus7_cb)
st.button('Times 2', on_click=_make_times2_cb)

st.write(f'Start = {start} | End = {end}')

Happy coding!
Arvindra

1 Like

Hi @Rishikesh , as @asehmi says, date/time is a pain.

I am going with the assumption that for

  1. today: start & end date = today
  2. week: start & end date = 1st and last day of current week
  3. month: start & end date = 1st and last day of current month

If this is not so, please change the code accordingly.

import streamlit as st
from datetime import datetime as dt
import calendar as clndr

def ChngDt(wch_option):
    if wch_option == "Today":
        st.session_state.ksd = dt.now()
        st.session_state.knd = dt.now()

    elif wch_option == "Week":
        myclndr = clndr.monthcalendar(dt.now().year, dt.now().month)
        tdy = dt.now().day
        for i in range(len(myclndr)):
            if tdy in myclndr[i]:
                st.session_state.ksd = dt(dt.now().year, dt.now().month, myclndr[i][0])
                st.session_state.knd = dt(dt.now().year, dt.now().month, myclndr[i][6])
                break

    elif wch_option == "Month":
        st.session_state.ksd = dt(dt.now().year, dt.now().month, 1)
        st.session_state.knd = dt(dt.now().year, dt.now().month, clndr.monthrange(dt.now().year, dt.now().month)[1])

vstdt = dt.now()
vnddt = dt.now()
wch_option = ""

sc11, sc12 = st.columns(2)
stdt = sc11.date_input("Start Date:", key="ksd")
nddt = sc12.date_input("End Date:", key="knd")

sc21, sc22, sc23 = st.columns(3)
if sc21.button("Today", on_click=ChngDt, args=("Today",)):
    wch_option = "Today:"

if sc22.button("Week", on_click=ChngDt, args=("Week",)):
    wch_option = "This Week:"

if sc23.button("Month", on_click=ChngDt, args=("Month",)):
    wch_option = "This Month:"

vstdt = st.session_state.ksd
vnddt = st.session_state.knd

st.write(f"{wch_option} Start Date: {vstdt:%d-%b-%y} | End Date: {vnddt:%d-%b-%y}")
4 Likes

Nice job @Shawn_Pereira :balloon:

1 Like

Thanks @asehmi , I learnt from all you guys, and passing the learning along :slight_smile:

God Bless

4 Likes

Thank you @Shawn_Pereira & @asehmi
I just started learning Streamlit & learning basic things.
thanks for your support :smile:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.