Caching uploaded file while using multipage

Hi , I have hard time caching my uploaded file, I want to used it on all of the others pages of my application, the user upload the source file on the app main page.
The problem is that everytime that I change page it clears the data that have been uploaded previously.

Here’s what the source code of main page looks like:

uploaded_f = st.sidebar.file_uploader(‘Choose a XLSX file’, type=[‘xlsx’, ‘xlsb’])

if uploaded_f:
@st.experimental_memo(suppress_st_warning=True)
def kimiwa():
uploaded_file = uploaded_f
return uploaded_file

and for one of the page that will be exploiting it

uploaded_file = kimiwa()

@st.experimental_memo(suppress_st_warning=True)
def corridors():
df = pd.read_excel(uploaded_file, sheet_name=‘croisé’, engine=‘openpyxl’)
return df

if uploaded_file:
corridors = corridors()
print(corridors)

Hi, I am also facing the same problem. Did you get any solution to this?

Hi @khalifa1999 and @Ishtiak_Ahmed :wave:

Here’s an example of caching an uploaded file in a multipage setting, based on your code. When switching pages, the uploaded data is preserved and not cleared:

Code

page_1.py
import streamlit as st
import pandas as pd

uploaded_f = st.sidebar.file_uploader("Choose a CSV file", type=["csv"])

if uploaded_f is not None:
    @st.experimental_memo
    def kimiwa():
        uploaded_file = uploaded_f
        return uploaded_file
pages/page_2.py
import streamlit as st
import pandas as pd
from page_1 import kimiwa # import the function from main page

uploaded_file = kimiwa()

@st.experimental_memo
def corridors():
    df = pd.read_csv(uploaded_file)
    return df

if uploaded_file is not None:
    corridors = corridors()
    st.write(corridors)

Save the following as data.csv:

data.csv
Col1,Col2,Col3
A,B,C
X,Y,Z

Output

mpa-cache-upload

Best, :balloon:
Snehan

1 Like

@snehankekre it works perfectly sir, can’t thank you enough for your help, it really made my night. Hope that your solution will benefit many other people like @Ishtiak_Ahmed aswell.

1 Like