Save session specific uploaded file

Hi Team,

Thanks for this amazing application, I have been using streamlit in every project now to visualize and share it with others.

I have a use case, and I am not sure how to solve for it. I want to share my app with multiple people, who can have the option to upload a csv file containing multiple user_ids, and for each session, I want to read in the file and show the user_ids as radio button on the sidebar. The user can select any user_id and see the details for that id (which is fetched from locally saved data/ S3).

The problem is whenever the user selects one of the ids from the side bar, the app refreshes and prompts for file upload again. If i use st.cache, then this caches a certain file for all subsequent sessions. I am not able to figure out how to use SessionState for the same.

Any guidance is appreciated.

Cheers!

Hello Streamlit community,

I also have the same problem. I would like to keep the file upload saved so that i can use it in my app with multiple pages. Whenever I click on next page and get back again I have to re-upload the file again and all results disappear once I get from a page to an other.
Please if someone has an idea how to use file upload with session-state with multi-page Streamlit app please share the solution with us.

Thank you!

@Ribi @sourav
assign different keys for different users, problem can be solved.

import streamlit as st
userid=st.sidebar.radio("Choose your user ID",("user_id1","user_id2","user_id3","user_id4","user_id5"))
if userid == "user_id1":
	upload1=st.file_uploader("Choose your file to upload", key="1")
elif userid == "user_id2":
	upload1=st.file_uploader("Choose your file to upload", key="2")
elif userid == "user_id3":
	upload1=st.file_uploader("Choose your file to upload", key="3")
elif userid == "user_id4":
	upload1=st.file_uploader("Choose your file to upload", key="4")
elif userid == "user_id5":
	upload1=st.file_uploader("Choose your file to upload", key="5")

I use my 2 exploers to simulate two different users to do the file upload at the same time when they choose different user id, their upload option is independent, no influence occur between different users.

@BeyondMyself
What I mean is having only one file_uploader in the first page and then when I move to the next page to do some functions which process that file, it does not save the state.
Navigation wise, my uploaded file will disappear once I click on the second tab/ page of my app.
So I would like to have something which saves me the file uploaded and keep it loaded.
And I am also faced with the problem of when I click a button on the same page with my file uploader it also starts from scratch and I have to upload again.

Please let me know if I explained well the situation and thanks a lot for the help :slight_smile:

file=st.file_uploader()
df=pd.read_csv(file)
you can use this df any where in your app, it has no relationship with you use mulitipage or not.

I solved it using this code based on session_state and this way my dataframe is recognised in my other pages ( page 1 : file upload , page 2: data processing on my data )

code:

file = file_upload()
		
		if file:
			if 'load_csv' in st.session_state:
				df=st.session_state.load_csv
				st.write(file.name + " " +  "is loaded") 

			else:
				df = load_csv(file)
				st.session_state.load_csv = df

Hey, I have a similar problem. I have 6 pages, and I am looking to upload a dataframe in the first page, and it should persist across all remaining pages.

Just confused, should I add this piece of code to every page (every python script), or just the first one?

have you solved the problem? if yes can i see the full code? I’m still confused about the command ‘load_csv’

Hello, maybe I’m a little late, but I want to share the solution I found:

You must simply assign the uploaded file to a session state variable in python, as shown below:

Carga_archivo = st.file_uploader(“:open_file_folder: Seleccione el insumo”,type=[“xlsx”,“csv”])
if Carga_archivo is not None:
# Read the uploaded file as a Pandas DataFrame
#na_fwl: nombre de archivo fwl
df_insumo = pd.read_excel(Carga_archivo)
# Describe el arvhivo cargado y muestra en formato tabla su contenido
st.session_state[‘df’]=df_insumo
st.session_state[‘name_file’]=Carga_archivo.name