How can I use dataframes in different pages

I’ve a project about some datas and I will use Streamlit for it.

I got the datas from Excel (Node Number Coordinates and Members) and I’ve showed them as you can see at the bottom.

However, I want to plot this datas in second page which is called by “Frame Contacs” but I couldn’t get the dataframes which is located in first page (Nodes)

I 've checked the internet and foun “Session_state” so am I need to use this things or is there any easy way to do it ?

The basic logic is:

# Check if you've already initialized the data
if 'df' not in st.session_state:
    # Get the data if you haven't
    df = pd.read_csv('my_file.csv')
    # Save the data to session state
    st.session_state.df = df

# Retrieve the data from session state
df = st.session_state.df

Session state acts like a dictionary. If you are only loading data based on some user input/action, then you need to tuck that information away in session state before they leave the page e.g.
st.session_state['some key'] = df

On another page, you can get that variable back with
df = st.session_state['some key']

Just be aware that trying to retrieve something from session state that isn’t there will cause an error, that’s why it’s best practice to check if the key is in session state at the top of page. If it’s not appropriate to force a default value for it on some other page, just make sure to handle the error.

if 'some key' not in st.session_state:
    st.warn('Please go back to page 1 and select the data.')
else:
    # The rest of your plotting page
    pass
3 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.