Help with cache, callbacks or session state

Hello community,

I’m trying to implement some sort of memory into my app, but I’m struggling to understand exactly how to do it and which component I should use.

Here’s how it works:
I have a multipage app, done with the latest native support from streamlit.
On each page, I can drag&drop a file (different type of file on each page but always a .csv, log, or text file). The drag & drop widget is on the page, not on a sidebar.
On each page, the uploaded file gets analyzed, some calculations are performed then a bunch of plots are made.

The issue is that for now, when I switch to another page, I loose the results (file and results) on the previous page I was on and need to re-upload the input file if I want to see the results again.

I have a sidebar with either links to markdowns or widgets (sliders or info box).

By reading the documentation, I feel like the session state is not what I should use, but more the cache function.

Any help on how I could implement that, to make my app more user friendly?

Multipage and session_state do not yet seem to play too well together: widget state not synced between multiple pages · Issue #4989 · streamlit/streamlit · GitHub . One can work around this by creating extra variables not related to a widget but this somewhat messes the code… I also hope this gets resolved soon.

To clarify, the issue that is reported in that Github issue is not about session_state per se, but rather about session state entries that come automatically from widgets (e.g. if I do st.checkbox(..., key=123) that automatically generates an entry in st.session_state called 123, but that session state entry might not persist across pages).

That being said, my recommendation would be:

  1. Use session_state to keep track of the files that have been uploaded
  2. Show the files that have previously been uploaded in the sidebar. If a user then uploads a new file, overwrite that file.
  3. If you need to do something with the file, then you can access it from st.session_state on any page.

Here are two examples of what that might look like:

from io import StringIO

import pandas as pd
import streamlit as st

my_csv = st.file_uploader("Upload csv", type="csv")

if my_csv is not None:
    st.session_state["my_csv"] = my_csv.getvalue().decode("utf-8")

if "my_csv" in st.session_state:


import streamlit as st

my_image = st.file_uploader("Upload image", type="jpg")

if my_image is not None:
    st.session_state["my_image"] = my_image

if "my_image" in st.session_state:

Note that if you switch pages, the uploaded files are still saved, and don’t need to be re-uploaded.