Following Streamlitās documentation, Iāve created a form to receive some input and I would like to use the result of such input on the next steps of the app once the submit button is clicked. So, Iāve called a function to save some variables in session state using the āon_clickā callback as depicted in the code below:
with st.form(key="filtering_form_{}".format(step)):
st.subheader("Raw Data Sample")
st.write(useful_threats[BASIC_ATTRIBUTES].sample(20))
available_threat_types = f_utils.get_sorted_feat_unique_vals(useful_threats,'threat_type')
chosen_threat_type = st.selectbox(
"Which threat type do you want to analyze?",
available_threat_types
)
st.form_submit_button(
label="Filter Threats",
on_click=w_utils.set_step_vars(
{"filtered": True,
"threat_type": chosen_threat_type},
step
),
)
The problem Iām facing is: when I first load the application, without even clicking the form submit button, the āon_clickā callback button is executed (I think it happens at widget rendering time) and the variables get set on session state before they should. This misleads the application control flow and thus it behaves incorrectly.
Am I doing something wrong? Is this a known issue?
How should I proceed to make sure the code inside āon_clickā callback is only executed on button click?
Unfortunately, I did not find any solution yet.
Had to tweak the session cache usage to achieve what I wanted without the use of submit buttonās on_click callback.
If you want the callback to execute on click, pass the function name to the on_click parameter instead of a function call with arguments. Hereās a working example:
import streamlit as st
def callback(v):
st.write(v)
bt1 = st.button(
"test1",
key=None,
help="help info",
on_click=callback, # only the function name
args=("aaa",), # include args here, not above
kwargs=None,
disabled=False,
)
You want to do the above instead of passing the arguments in the on_click assignment:
# bug
import streamlit as st
def callback(v):
st.write(v)
bt1 = st.button(
"test1",
key=None,
help="help info",
on_click=callback("aaa"),
kwargs=None,
disabled=False,
)
I guess itās not clear to me exactly how you are getting your argument for the function.
Is it from a user input? If from a user input the way I suggested works really well for me.
Otherwise I suggest adding your function input variable to session state and remove args.
You can test the code I linked to (or your own) online here to see what it does: