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:
my_app
βββ streamlit_app.py <-- Your main script
βββ pages
βββ page_2.py
βββ page_3.py
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.
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β
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.