Multi-page app

I’m trying to use the new session_state system to build a multi-page app but between the pages the session state is not shared. How to fix that ?
Thanks !

Hey @antonin-lfv,

Can you post a link to your code?

Happy Streamlit-ing!

Here it is ! streamlit_multi_page/ at main · antonin-lfv/streamlit_multi_page · GitHub
The pb is that I have to change the page to apply the modification on the state
For example, if I choose a data frame in the file_uploader, and then I choose another one, I have to change the page to plot the new data frame
Is there a solution ?

May I shamelessly suggest you use a library specifically created to make multi-page apps easily. Hydralit, it has internal state management as you can see from the live example application located in the hydralit-example repo. You can see a running example:


I just had a quick look but I could not figure out where you really set the page to run. Also I think that fileselector object is a stream. If you want to use it on ‘another page’ you should store this object as well in the st.session_state. Here is a minimal sample how the page switching works for me:

import streamlit as st

def page_switcher(page):
    st.session_state.runpage = page

def main():
    btn = st.button('goto page1',on_click=page_switcher,args=(page1,))
    if btn:
        st.experimental_rerun() # rerun is needed to clear the page

def page1():
    btn = st.button('go back')
    if btn :
        st.session_state.runpage = main        

if __name__ == '__main__':
    if 'runpage' not in st.session_state :
        st.session_state.runpage = main
1 Like

I solved the transition between “pages” → apps, by capturing the target app state using the session then kicking the host with rerun, inside the host, it will then find the target app from the session and load it, code can be found in the _run_selected() method. For Hydralit, I use a separate loader app, this loads quickly then freezes the screen items to reduce the amount of overlap in items between the from → to app, then disappears and the target app remains, it can also be used to customise how app transitions occur.

1 Like

Thanks everyone for your suggestions!

And how do you share a dataframe from file uploader in a page, for example, through all the pages ?
And modifies it in a page for all pages ?
should I use st.session_state ?

Hey @antonin-lfv,

I would just put the data frame in your session state as well. That way on one page you can modify the data, and then the last thing you do is save it in st.session_state and then on the other pages you can grab the saved data from the session!

here is a silly example of that:

import streamlit as st
import pandas as pd

# create pages
page ='Pages', ['First Page', 'Second Page'])

# create dataframe
if 'df' not in st.session_state:
    d = {'col1': [1, 2], 'col2': [3, 4]}
    st.session_state['df'] = pd.DataFrame(data=d)

if page == "First Page":
    # do some stuff to the data
    st.session_state.df['col3'] = st.session_state.df['col1'] * st.session_state.df['col2']
elif page == 'Second Page':
    st.session_state.df['col4'] = st.session_state.df['col1'] * st.session_state.df['col2']

Thanks to @Probability and @ksxx for dropping those helpful suggestions in my absence! :raised_hands:

Happy Streamlit-ing!


Thanks ! This solution is my final option, I wanted to have different functions for each page, but your solution works, thx again

1 Like