简体   繁体   中英

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.

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 .

tips: use st.session_state to save data

code example


import streamlit as st 
from datetime import datetime

def my_callback():
    print(f"curdate: {datetime.now()} first: {st.session_state.value1} sec: {st.session_state.value2}")

with st.form('my_form',clear_on_submit=True):
    st.radio(label='🙆‍♀️ part1:', options=[
                        '解决', '未解决', '未完全解决'], horizontal=True,
                        key='value1')

    error_list = ['答非所问', '推荐错误', '推荐不准确', '回答不详细', '信息更新不及时']
    not_saf_reason = st.radio(
        label='🤦‍♀️ part2:', options=error_list, horizontal=True,key='value2')

    submit_button = st.form_submit_button(
                    label='📥 点我提交', on_click=my_callback
                    )
  1. save part1 to value1 which is a var in st.session_state
  2. save part2 to value2 which is a var in st.session_state
  3. write a callback function to get value1 and value2 from st.session_state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM