Last active
May 19, 2022 12:44
-
-
Save asehmi/89ab6de8c4417a22c5cf294ef904246f to your computer and use it in GitHub Desktop.
Setting Streamlit widget values via callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generic answer to this question: https://discuss.streamlit.io/t/how-to-work-date-input-with-shortcuts/25377 | |
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}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment