'I am using Streamlit to build a form that based on answers builds another form, how do I hide the form so they don't stack?

I am building form using streamlit. I use the session_state to control what section of the questions to appear. This is section 1 but there are over 10 sections, not all sections get asked, it's based on you how you fill out the last section.

    if 'section' not in st.session_state:
      st.session_state['section'] = 'section 1'

    if st.session_state['section'] == 'section 1':
      with st.form(key='Scoresheet', clear_on_submit=False):
        st.markdown("""
          ### ScoreSheet
          Repeatable sheet for Grading tech tickets
          """)
        tech_name = st.text_input("Tech Name")
        date = st.date_input("Date")
        ticket_number = st.text_input("Ticket Number")
        phone_availability = st.radio("Phone Availability (Weekly)",['<10','20','30','40','50','60','70','80','90','100','N/A Leads // Special Project'])
        outage = st.radio("Large Outage",['TSP','Large Outage','Protedcted Sonet','Protected Wave','Point to Point SONET','Point to Point Wave'])
        next1 = st.form_submit_button(label='Next')

      if next1:
        st.session_state['Data1'] = {'tech_name': tech_name, 'date': date, 'ticket_number': ticket_number, 'phone_availability': phone_availability, 'outage': outage}
        if outage in ['TSP']:
          st.session_state['section'] = 'section 2'
        elif outage in ['Large Outage']:
          st.session_state['section'] = 'section 3'
        elif outage in ['Protedcted Sonet','Protected Wave']:
          st.session_state['section'] = 'section 4'
        elif outage in ['Point to Point SONET','Point to Point Wave']:
          st.session_state['section'] = 'section 5'

When you click submit it renders the next section's form underneath the existing form. This happens for each section, unless you click the menu item for the questionnaire, in which rebuilds the page with just the current section.

I would like it to clear out the previous section and just show the current section.



Solution 1:[1]

Use a placeholder for the form, once submitted empty it.

placeholder = st.empty()

with placeholder.form(key=str(num)):
    ...

    if st.form_submit_button():
        placeholder.empty()

Check this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ferdy