Caching uploaded file while using multipage

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