Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streamlit : How to reload a page using a button and storing previous informations after each click

The aim of my application is to build a (downloadable) tsv file after filling a form. Each form page is related to a new student's information. So I always have the same page structure (same form for each student). What I want is adding a button that generates a new page for each new student. At the end, an "end" button should display the Table (using for instance st.dataframe()) of all students.

The following script generates one single page (toy example):

import streamlit as st
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")
    
new_student = NewStudent(page_id=1)

I've been testing many solutions, but all the solutions add an NewStudent in the same page. Thank you in advance.

like image 313
NED Avatar asked Dec 11 '25 23:12

NED


1 Answers

Create a loop to process student info. All info will be saved in a list which will be converted to a dataframe later.

Code

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

    while True:    
        num = st.session_state.num

        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})
                    st.session_state.num += 1
                    placeholder.empty()
                    placeholder2.empty()
                else:
                    st.stop()

main()

Output

Fillup then press register to record info to a list of dictionary. enter image description here

Fillup again. enter image description here

Then press end. enter image description here

Then the saved data will be displayed.

enter image description here

like image 194
ferdy Avatar answered Dec 14 '25 13:12

ferdy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!