Query related to checkboxes under different pages?


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


Additional information

So, I am requesting anyone can help me to solve my error. If anyone give the solution are requested to mail to sekharchennaiah12345ctk@gmail.com

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'])

Thanks for your explanation. Can u give some more detail?

What part are you struggling with?

  1. 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.
  2. 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.
  3. Copy from the placeholder/copy key back to the original (from _my_key to my_key).
  4. Render your widget with key 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:-

result=st.selectbox(‘choose the following
if result==‘a’:
st.checkbkx(‘please select check box 1’)
st.success(" Successfully check box was

elif result==‘b’:
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

thank you
Guna sekhar.

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.