State management with st.form_submit_button

This is running locally.

I have the following code

if "submitted" not in st.session_state:
    st.session_state.submitted = "Not Clicked"

def clicked():
    st.session_state.submitted = "Clicked"

st.write(st.session_state.submitted)

with st.form("test form"):
    st.form_submit_button("Submit", on_click=clicked())

st.write(st.session_state.submitted)

When I start the app up using streamlit run, this is what it initializes as:

I have no interacted with the app at all. Why is the value of st.session_state.submitted being changed?

Just use on_click=clicked

That worked! Thank you @ferdy!

Can you explain why?

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