Form_submit_button's on_click being called before clicking

Hi everyone,

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?

1 Like

I’ve a similar issue with all my buttons ,not just forms.
Did you find a solution?
It activates all on_click functions without a click event

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.

Hi all :wave:

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,
)

button-onclick

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,
)
3 Likes

there is another problem by using this.

The callback get’s input only after second click

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,
)

st.write provides first time empty output

Try setting a key for the input text, then writing the session state object with that key.

so your recommendation is not to use args?

and sorry there was a copy/paste error:

def callback(v):
st.write(v)

bt1 = st.button(
“test1”,
key=None,
help=“help info”,
on_click=callback,
args(“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:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.