when having a form when submit button clicked, args are handed over after second click only.
Taken a simple form
with st.form(key='Zählerdaten', clear_on_submit=True):
add_strom = st.number_input('Strom', )
add_gas = st.number_input('Gas')
add_wasser = st.number_input('Wasser')
# Every form must have a submit button.
submitted = st.form_submit_button("Speichern",
on_click=add_measurement,
args=(add_strom, add_gas, add_wasser, df),
kwargs=None)
Looks like the callback is receiving the previous value of the widget, not the current one. Do you really need a callback here? The more straightforward method works as expected.
import streamlit as st
with st.form(key="form", clear_on_submit=True):
number = st.number_input("Number")
submitted = st.form_submit_button("Submit")
if submitted:
st.write(number)
…no, the alternative you proposed would work as well.
Nevertheless the method does some more data updates in the background, therefore it seems to be straight forward couple the method to a click event.
Looks like the callback is receiving the previous value of the widget
This is the reason why I raised it up here. I would like to understand if it works as expected (and then why only the previous value is handed over) or if we have a bug here.
It’s not a bug I don’t think, but it’s not clearly explained in the documentation.
Basically, the variables in the select aren’t written to the variable names until you choose an option from the drop-down, but the callback is also executed on change and appears to run first.
That’s why we have to use the widget key in the callback function.
Definitely could be made clearer in the docs with another example or two, but using keys is a nice solution
Edit: seems there is some discussion/documentation around this
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.