When I submit a form with 2 date inputs for start and end dates, I am not able to get the values that I select. Not sure at all what I am doing wrong.
Code snippet:
def calendarForm(mindate,maxdate):
with col2:
with st.form(key="metercalendar",clear_on_submit=True):
sd = st.date_input('Select a Start Date (Earliest date is shown)',value=mindate,min_value=mindate,max_value=maxdate,key='startDate')
ed = st.date_input('Select an End Date (Latest date is shown)',value=maxdate,min_value=mindate,max_value=maxdate,key='endDate')
submitBtn = st.form_submit_button('Get Hourly Consumption',on_click=getHourly,args=(sd,ed))
def getHourly(sd,ed):
col3.write('---'+str(sd)+'----'+str(ed))
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
sd and ed should have the dates that I select and not the default dates with which the widget is initialized Actual behavior:
I always get sd and ed to be the default dates with which I initialize the widget.
Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.
Debug info
Streamlit version: 1.14.1
Python version: 3.10.6
Using PipEnv
OS version: Ubuntu 22
Browser version: Google Chrome Version 107.0.5304.110 (Official Build) (64-bit)
Requirements file
Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.
Thanks for sharing your question! Can you share a more complete code snippet (i.e. where you’re calling the functions you’ve defined) so we can reproduce the issue?
you create a form and submit button, and via another callback
you run your getWeeklyTWA function, feeding it the outputs from your form that were present when they were created on the page, not when they were submitted
When you put arguments into a callback, they will be evaluated at the time the widget with the callback was created and not at the time the widget is clicked. In case of your form, you have specified a default value equal to the minimum for your two dates, so the submit button is all ready to run your getWeeklyTWA function with those default minimum values no matter what the user does to change the selection before clicking submit.
In general, I shy away from creating widgets within a callback. (Not that it can’t be done, but I usually find it a bit clearer and more direct to get at data associated with widgets outside of callbacks.) As soon as a widget disappears, so too does its data/state and you’ll never have any use for storing the output of a widget if it does not remain on the screen to feed the updated value to that variable receiving the output.
@mathcatsand I see. But it’s a little confusing because if that is the case what does the submit button for my form do? So for e.g. if a form is created, and a widget is created inside it, and the widget has a default value, but I change the value on the widget, shouldn’t the form submit get the latest value?
Is the behavior an artifact of the callback function or the default behavior in Streamlit?
Thanks for helping!
When you click a form submit button, all of the widgets within the form will update. You have two options of retrieving the information:
By having variables take in the output of the widgets
By collecting it from session state
However, if the widgets do not exist on the page after a submit button is clicked, option 1 is out the window. Furthermore, you will be restricted on how you implement option 2 as the information (as natively stored) will only be available for one page load, then gone.
As such, a better workflow with a submit button if you are having the form disappear is to use a callback function to grab and store that information for later use.
Here’s a little toy case so you can see what happens to session state when a form disappears from view (Show/Hide button) and what happens with reloads/other interactions with the page (Page Reload button). In particular if you submit a form, hide it, then click the reload button, you’ll see the information associated directly to the widgets goes away, but the copy I made in the callback to other keys will stay.
import streamlit as st
st.session_state
def submit ():
st.session_state.last_A = st.session_state.A
st.session_state.last_B = st.session_state.B
if 'show' not in st.session_state:
st.session_state.show = True
def toggle():
st.session_state.show = not st.session_state.show
st.button('Show/Hide Form', on_click=toggle)
if st.session_state.show:
with st.form('my_form'):
st.text_input('Text', key='A')
st.number_input('Number', key='B')
st.form_submit_button('Submit', on_click = submit)
st.button('Page Reload')
st.session_state
And yes, the logic flow of the callback is coming in to play, namely that the values you want to pass as arguments are evaluated at page load and not at click. So you have to get “new information” from inside the callback, with the lookup happening to session state after the click.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.