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?
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:
Use session_state to keep track of the files that have been uploaded
Show the files that have previously been uploaded in the sidebar. If a user then uploads a new file, overwrite that file.
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:
streamlit_app.py:
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:
st.sidebar.dataframe(pd.read_csv(StringIO(st.session_state["my_csv"])))
pages/page1.py
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:
st.sidebar.image(st.session_state["my_image"])
Note that if you switch pages, the uploaded files are still saved, and don’t need to be re-uploaded.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.