Hi @AntoPascarella,
Here’s some example code that does some very basic processing (grab the first 10 rows) and saves that processed data into st.session_state so that the data is saved and can be used across multiple pages
import streamlit as st
import pandas as pd
if "processed_data" not in st.session_state:
st.session_state.processed_data = None
uploaded = st.file_uploader("Upload CSV", type=["csv"])
if uploaded is not None:
df = pd.read_csv(uploaded)
st.session_state.processed_data = df.head(10)
st.write("Processed data", st.session_state.processed_data)
HOWEVER, if you want it to work across the app being refreshed, then you will need a different approach, because st.session_state will be reset when the app is refreshed.
How you handle that depends somewhat on if the data is meant to be specific for a given user, and if so, then how do you identify a given user. If you have some sort of sign-in, then you could save the data to a file on disc and use the user’s name to set the file name, so every person would get a distinct file.
Just to make a simple example, here’s a way to save the file to disk based on the name the user provides.
import streamlit as st
import pandas as pd
from pathlib import Path
def get_data_from_disk(username: str) -> pd.DataFrame:
return pd.read_csv(f"fake_data/{username}.csv")
def save_data_to_disk(username: str, data: pd.DataFrame) -> None:
# Create fake_data folder if it doesn't exist
Path("fake_data").mkdir(exist_ok=True)
data.to_csv(f"fake_data/{username}.csv", index=False)
username = st.text_input("Username")
if not username:
st.stop()
if "processed_data" not in st.session_state:
try:
st.session_state.processed_data = get_data_from_disk(username)
except FileNotFoundError:
st.session_state.processed_data = None
uploaded = st.file_uploader("Upload CSV", type=["csv"])
if uploaded is not None:
df = pd.read_csv(uploaded)
st.session_state.processed_data = df.head(10)
save_data_to_disk(username, df)
st.write("Processed data", st.session_state.processed_data)
NOTE that if your app is hosted on community cloud, and perhaps other hosts as well, files saved to disk are not guaranteed to persist forever, so this won’t guarantee that the user’s file stays around forever. If you want that, then you probably want to use an external database to store the user’s data permanently.