How to do a sequence of forms

Summary

How do I do a sequence of forms? I want the user to fill out a form, the app computes and answer, and then the user can submit another form based on that answer.

Steps to reproduce

Code snippet:

form = st.form("my_form")
text = form.text_area("put stuff", example, height=200)
submit = form.form_submit_button("Submit")
if submit:
    # do stuff
    form_2 = st.form("my_form2")
    text2 = form_2.text_area("put stuff2", example, height=200)
    submit2 = form_2.form_submit_button("Submit")
    if submit2:
        st.write('stuff based on form2')

Expected behavior:

After filling out the form the user can fill out the second one and then see the results

Actual behavior:

after submitting the second form the system goes back to the original state

Hey @Jonathan_Mugan,

You can do this using session state, i.e.:

import streamlit as st

# Initialize session state
if 'first_form_completed' not in st.session_state:
   st.session_state['first_form_completed'] = False

with st.form("my_form"):
    input = st.text_input("Example")
    if st.form_submit_button("Submit"):
        st.session_state['first_form_completed'] = True

if st.session_state['first_form_completed']:
    with st.form("my_form2"):
        input = st.text_input("Example")
        if st.form_submit_button("Submit"):
            st.write('stuff based on form2')

FormExample

Thanks @Caroline! I’ll try that out!

Yep, that worked! Thanks!

1 Like

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