How to have sequential forms?

Using Streamlit 1.28.2 and Python 3.11:

import streamlit as st


st.title("Test")
with st.form("form-1"):
    submitted_1 = st.form_submit_button(label="Submit")
    if not submitted_1:
        st.stop()
    st.write("1")
with st.form("form-2"):
    submitted_2 = st.form_submit_button(label="Submit")
    if not submitted_2:
        st.stop()
    st.write("2")

This doesn’t work, submitting the second form just clears the screen. How can one get sequential forms to work?

Multiple forms in a page seems related but is unanswered.

Also, same issue still occurs if you replace the second form with a button instead

Hey @jamesbraza,

Check out the example in this thread:

1 Like

From Part of page is getting refreshed on dropdown selection - #2 by andfanilo, it’s revealed the solution is to use st.session_state:

import streamlit as st


st.title("Test")
with st.form("form-1"):
    submitted_1 = st.form_submit_button(label="Submit")
    if not submitted_1 and not st.session_state.get("submission-1"):
        st.stop()
    st.session_state["submission-1"] = True
    st.write("1")
with st.form("form-2"):
    submitted_2 = st.form_submit_button(label="Submit")
    if not submitted_2:
        st.stop()
    st.session_state["submission-1"] = False
    st.write("2")

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