To achieve this, you can use Streamlit’s session_state to store the loaded data and make it available across different pages of your multi-page app. Here’s an example of how you can structure your code:
import streamlit as st
import pandas as pd
# Define a function to load the data
@st.cache
def load_data():
df1 = pd.read_parquet('df1.parquet')
df2 = pd.read_parquet('df2.parquet')
df3 = pd.read_parquet('df3.parquet')
df4 = pd.read_parquet('df4.parquet')
return df1, df2, df3, df4
# Define a function to set up session state
def setup_session_state():
if 'loaded_data' not in st.session_state:
st.session_state.loaded_data = load_data()
# Page 1: Load Data
def page_load_data():
setup_session_state()
df1, df2, df3, df4 = st.session_state.loaded_data
# Use the loaded data in the rest of the page
st.write("Page 1: Load Data")
st.write("Data Frame 1", df1)
st.write("Data Frame 2", df2)
st.write("Data Frame 3", df3)
st.write("Data Frame 4", df4)
# Page 2: Use Data
def page_use_data():
setup_session_state()
df1, df2, df3, df4 = st.session_state.loaded_data
# Use the loaded data in the rest of the page
st.write("Page 2: Use Data")
st.write("Data Frame 1", df1)
st.write("Data Frame 2", df2)
st.write("Data Frame 3", df3)
st.write("Data Frame 4", df4)
# Sidebar to switch between pages
page = st.sidebar.selectbox("Select a page", ("Load Data", "Use Data"))
# Display the selected page
if page == "Load Data":
page_load_data()
elif page == "Use Data":
page_use_data()
the data is loaded only once, and subsequent pages can access the loaded data from the st.session_state.loaded_data attribute without rerunning the data loading function.
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.