I am looking to pass variables between pages in a multi-page application. It looks like others did this previously (Multi-page app with session state) before the launch of the Multi-page application in June 2022 (Introducing multipage apps! 📄). Has anyone come across a method for passing variables between pages using the new architecture?
My application is setup with:
├── streamlit_app.py <-- Your main script
I want the user to make selections on each of the pages that then updates the others by propagating the selections that have made previously. Additionally, I would like the input values to remain if a user navigates away from a page and then back to it.
Any help here would be great!
Ended up figuring it out. Turns out that the st.session_state will propagate across pages under the new structure.
For reference on st.session_state - Session State - Streamlit Docs
Hi @ryanczarny, I am trying to do similar thing. Let’s say I have a variable in page 2 which I pass in page 3. However, my idea is if that variable (a user input) is equal to a specific value then it does a specific task in page 3 else it does another task. For first time it is doing in exact way what I want but if the user change the input (variable), the session_state still holds the old value. So Instead of the other task, it still do the same task.
hey @DebayanPaul93, I ended up finding a way around that but having buttons that either save the filter value or erase the session value.
Before adding a new input I have the code do the below -
if 'user_select_value' not in st.session_state:
st.session_state['user_select_value'] = 0 #or whatever default
user_select_value = st.session_state['user_select_value']
I then add the below -
if st.button('Save Filters'):
st.session_state['user_select_value'] = user_select_value
if st.button('Clear page Filters'):
st.session_state['user_select_value'] = 0 # or default value
There might be a better way of doing it, but that is how I got it to work for me.
Hi @ryanczarny, I ended up declaring the variable which I want to pass in global scope in page 2 (first input page) and return it through a function in the other page (second page)
Hi @DebayanPaul93 , do you mind sharing what that looks like on the second page (the one with the function that calls up the originally declared variable)?
Hi @jbonefish , as far I remember it was an user input variable which I wanted to pass from page1 to page 2, but it doesn’t need to show up in page 2. The purpose was to display another input based on value of the variable in page 1 (IF/ELSE logic). For this you need to define the variable in global scope and in page 2 have to import it something like “import page1.variable”