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:
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.