I have four main pages in my web app. Suppose, if the user visited to the fiest page and checked all the check boxes and he was moved to next page. Then for one moment he want visit the previous page. But the check boxes which he had checked in the past was not checked. But result of those checked boxes are storing in a session variable for the further purpose
When a widget with a key becomes not rendered, the key in session state is removed. This is true if the widget is conditionally not rendered while staying on the same page, or switching to another page (even if that new page creates a new widget with the same key).
Thus, to keep hold of a widget value through hiding it or switching pages, you need to copy that widget state in session state one way or another.
For example:
import streamlit as st
def keeper(key):
st.session_state['_'+key] = st.session_state[key]
if '_my_widget' not in st.session_state:
# Set initial value
st.session_state._my_widget = False
# Copy data from copied key to the widget's actual key
st.session_state.my_widget = st.session_state._my_widget
st.checkbox('A label', key='my_widget', on_change=keeper, args=['my_widget'])
For any widget with some key my_key, use a callback function to copy the widget state on_change from my_key to _my_key.
Before that widget is rendered, check if the placeholder/copy key exists. If it doesn’t, initialize the value tied to _my_key with your starting value.
Copy from the placeholder/copy key back to the original (from _my_key to my_key).
Suppose if I have two pages. First page contain a simple checkbox and second page contain simple selectbox. Firstly, i visited the first page and checked the check box and move to the second page. If I visit first page again, the check box which I had checked in the earlier was not selected but I’m storing it’s result in session variable. So, requesting you to explain the logic according to my code.
The following is my code:-
app.py:-
menu=[a,b]
result=st.selectbox(‘choose the following
page’,menu)
if result==‘a’:
st.checkbkx(‘please select check box 1’)
st.success(" Successfully check box was
selected"
elif result==‘b’:
sub=[“apple”,“banana”]
result1=st.selectbox(‘Choose the fruit’,sub)
…
So If I again visit to page ‘a’ then checkbox must in checked condition.
Can u explain my code and hoping that u can modify my code according to my condition
Try adapting the code I provided in my first response. Copy it onto two different pages. Change the widget to something else besides a checkbox if needed and then change the key to be unique on each page. (e.g. On the first page you would change everywhere you see my_widget to key_a and also _my_widget to _key_a. On the second page, you would change everywhere you see my_widget to key_b and also _my_widget to _key_b.) If you change to a different widget besides a checkbox, be sure to appropriately change the initial value where I’ve indicated in the comments.
Try giving it a shot. If you show your attempt, it will be easier to clarify the misunderstanding.
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.