Keep Uploaded File Between Pages

Hello,
I have an app with 2 pages: a homepage (parent folder) and page 2 (child folder of the within parent folder). The homepage has a fileuploader to upload and display a csv. This works, but when I switch to page 2, the back to homepage, the uploaded file has disappeared and I have to reupload. How do I use Session State to keep the uploaded file from disappearing when I go between pages?

Homepage, initial upload:

Page 2, displays data uploaded from Homepage:

Back to Homepage after visting Page 2, upload and dataframe have disapeared:

Homepage code:

%%writefile homepage.py

import pandas as pd
import streamlit as st
st.set_page_config(
    page_title = "Multipage App",
    layout='wide'
)

df_src = r"C:\Users\krakl\Jupyter\streamlit\strong (12).csv"
temp_data = pd.read_csv(df_src)
cols = temp_data.columns

st.title("Upload Data")
st.sidebar.success("Select a page")


# Allow only .csv and .xlsx files to be uploaded
uploaded_file = st.file_uploader(
    "Upload spreadsheet", 
    type=["csv", "xlsx",'txt'],
)
    
# Check if file was uploaded
if uploaded_file is not None:
    st.session_state['df_result'] = pd.read_csv(uploaded_file)
    

    # Work with the dataframe
    st.dataframe(st.session_state['df_result'].head(),use_container_width=True)
    
    wkt_count = len(st.session_state['df_result']['Date'].unique())
    st.write(wkt_count)

Page 2 code:

%%writefile page_2.py

import streamlit as st

st.markdown('page 2 test')

st.session_state['df_result']

Your help is appreciated, thank you.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.