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