st.form_submit_button does not wait for button press when form is called from a side button

I’m trying to create forms, each by using a side button.
When I do this, the form pops up, but the code that should occur AFTER the submit button is pressed is executed anyway. This makes it seem that the submit button does not wait and is set to FALSE.

Here is my simple code:

import streamlit as st

sideb = st.sidebar
formbtn1 = sideb.button("Click to start the form")
       
if formbtn1:
    sideb = st.sidebar

    with st.form(key='my-form', clear_on_submit=True):
        name = st.text_input('Enter your name')
        submit = st.form_submit_button('Submit')

    st.write('Press submit to have your name printed below')

    if submit:
        st.write(f'hello {name}')
        
    st.write('This is the text that should occur after the submit') 

What happens after the button is pressed to start the form, it’s as if it doesn’t wait for the submit_button to be pressed, and passes through the code and immediately writes “This is the text that should occur after the submit”. Also, when I do enter my name and hit “Submit”, nothing happens and all is lost.

Very stubborn issue. Any help would be appreciated.

The first button will revert to False after the second one is clicked. Check the 1. Buttons aren’t stateful section of the 10 most common explanations on the Streamlit forum for a more detailed explanation.

1 Like

FINALLY! Thank you for providing that link. It told me exactly what I needed to do, and I was able to finally have my side button start the form and it completed as expected now when I hit “submit”. I had seen other replies, but none of them were put into a simple format that I could adapt to mine. Thanks again! Cheers!

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