0

Considering this question as the continuation of the question in Streamlit : How to reload a page using a button and storing previous informations after each click, I would like to know how to empty a text input in a sidebar using the form_submit_button.

Thank you in advance.

(script after modification)

import streamlit as st
import pandas as pd


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


class NewStudent:
    def __init__(self, page_id):
        st.title(f"Student N°{page_id}")
        self.name = st.text_input("Name")
        self.age = st.text_input("Age")
    

def main():
    placeholder = st.empty()
    placeholder2 = st.empty()
    placeholder3 = st.sidebar.empty()
    placeholder4 = st.sidebar.empty()
    level_key = 1000
    major_key = 2000

    while True:    
        num = st.session_state.num
        level = placeholder3.selectbox("Level", ["L1","L2","L3"], key=level_key)
        major = placeholder4.text_input("Major", key=major_key)
        
        if placeholder2.button('end', key=num):
            placeholder2.empty()
            df = pd.DataFrame(st.session_state.data)
            st.dataframe(df)
            break
        else:        
            with placeholder.form(key=str(num)):
                new_student = NewStudent(page_id=num)        

                if st.form_submit_button('register'):                
                    st.session_state.data.append({
                        'id': num, 'name': new_student.name, 'age': new_student.age, "level": level, "major":major})
                    st.session_state.num += 1
                    placeholder.empty()
                    placeholder2.empty()
                    placeholder3.empty()
                    placeholder4.empty()
                    level_key += 1 
                    major_key += 1 
                else:
                    st.stop()

main()
NED
  • 111
  • 1
  • 4
  • 8
  • Where is your sidebar code? – ferdy Mar 13 '22 at 23:20
  • By using your script, I finally found a way by using multiple placeholders for each input boxes (select, text, ...). However, is there no other way to gather all the element in one single placeholder and empty then at once ? – NED Mar 14 '22 at 14:16

0 Answers0