'Streamlit : How to empty sidebar in parallel with a form?

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()


Sources

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

Source: Stack Overflow

Solution Source