Streamlit Multipage app

Hi All,

I have created streamlit multipage app and using some checkboxes which is disabling based on session.state and key.

The problem for me here is when i make some selection in page one and go next page it is holds the value from page one for session.state,
can anyone suggest me or reroute to the relevant post on how to clear or reset session.state before moving to next page in streamlit multipage app.

Page: -1

def disable_checkboxes():
        ## Dict with the disable conditions
        disable = st.session_state.disable
     
        ## Current checkboxes' values
        Que_9_O1_Yes,Que_9_O2_No, = (
            st.session_state.Que_9_O1_Yes,
            st.session_state.Que_9_O2_No,

            )
     
        ## Logic to disable checkboxes
        disable[1] = Que_9_O2_No
        disable[2] = Que_9_O1_Yes


    if x is not None and ("disable" not in st.session_state):
        st.session_state.disable = {1: Q9_O1, 2: Q9_O2, 3: Q9b_O1, 4: Q9b_O2, 5: Q9b_O3}  
    if "disable" not in st.session_state:
        st.session_state.disable = {1: False, 2: False, 3: False, 4: False, 5: False}
        
        
    "**✔️ Disabling Checkboxes and Session State**"
    disable = st.session_state.disable
	
	Question_9 = '''9. Does your company have a formal policy?'''
            # Display the question using st.write
            st.markdown('<div class="question">'+Question_9+'</div>', unsafe_allow_html=True)
            st.write("<br>", unsafe_allow_html=True)
            Question_9_Option_1i = st.checkbox('Yes',value =Question_9_Option_1_db, disabled=disable[1], on_change=disable_checkboxes,key= 'Que_9_O1_Yes')
            if Question_9_Option_1i:
                Question_9_Option_1='Yes'     
            Question_9_Option_2i = st.checkbox('No',value =Question_9_Option_2_db,disabled=disable[2], on_change=disable_checkboxes,key= 'Que_9_O2_No')
            if Question_9_Option_2i:
                Question_9_Option_2='No'

Now when am moving to page 2 where I have using similar function with different keys

Page 2 : - 
def disable_checkboxes():
        ## Dict with the disable conditions
        disable = st.session_state.disable
     
        ## Current checkboxes' values
        Que_10_O1_Yes,Que_10_O2_No, = (
            st.session_state.Que_10_O1_Yes,
            st.session_state.Que_10_O2_No,

            )
     
        ## Logic to disable checkboxes
        disable[1] = Que_10_O2_No
        disable[2] = Que_10_O1_Yes

if i select option 1 then in page 2 automatically option 2 is getting disabled.
Here I want to whenever i switch to page 2 or 3 or etc is should first clear the old session.

Hello,

If you have two functions, and you want a reset, just don’t use the same session_state’s name.
If you are using the same function, you can call you function with a name inside

def disable_checkboxes("YOURPAGENAME"):
        ## Dict with the disable conditions
        disable = st.session_state.disable['YOURPAGENAME']
     
        ## Current checkboxes' values
        Que_9_O1_Yes,Que_9_O2_No, = (
            st.session_state.Que_9_O1_Yes['YOURPAGENAME'],
            st.session_state.Que_9_O2_No['YOURPAGENAME'],

            )

And you can add a reset when you click to change your page, to remove all the session_state.

def clear_cache():
    keys = list(st.session_state.keys())
    for key in keys:
        st.session_state.pop(key)

st.button('Clear Cache', on_click=clear_cache)

Thanks Faltawer for sharing but its not working with my code.

Any alternate solution please

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.