Save the current Checkbox State while switching pages

Hello together,

I am new in using Streamlit and I am building my own app since last week.
Maybe I am a little bit confused, but I have the current problem:

I’ve created a checklist with multiple items on one page (Checklist page).
Within another page, I can open the calculator for some important results (Calculator Page).

Because I always have to switch between the Calculator- and Checklist page, I want to save the states of each checklist item. I tried to use the session.states but that does not work. After switching the pages, the checklist is still “On Reset”.

I only want to reset the checklist items using a separate button.

Could anyone help me to solve this problem?

Thanks a lot in advance!

Best Regards,
Dome

Hello, could you share your code? Using session_state might be the right approach. It could just be an issue with initialization or calling.

It is explained in the docs::

Hi @Faltawer,

thanks for your reply.
I tried to use the session state but it didn’t work yet. Maybe there are some mistakes from my side.

It always reset the checkboxes when I switch to another page (e.g. TOD Calculator) or if I switch between the Dropdown within the Navigation Section. I only want to use the button to reset within an active session - otherwise it should be saved.

Here’s the original code:

import streamlit as st
#from streamlit_option_menu import option_menu

# --- RESET FUNCTION ---
def reset_checklist():
    for key in st.session_state.keys():
        if key.startswith("cb_"):
            st.session_state[key] = False

# --- HEADER SECTION ---
st.title("Checklist - Flight Procedure")
if st.button("Reset Checklist"):
    reset_checklist()

# --- NAV SECTION ---
st.header("Navigation")

setting_cl_nav = "Dropdown"

if setting_cl_nav == "Dropdown":
    nav_category = st.selectbox(
        "Choose the Checklist Category",
        ("Preparation", "Pushback & Taxi", "Starting Procedure", "Cruise & ToD", "Approach"),
    )

if setting_cl_nav == "Slider":
    nav_category = st.select_slider(
        "",
        options=[
            "Preparation",
            "Pushback & Taxi",
            "Starting Procedure",
            "Cruise & ToD",
            "Approach",
        ],
    )

# Function to create a checkbox with persistent state
def persistent_checkbox(label, key):
    if key not in st.session_state:
        st.session_state[key] = False
    return st.checkbox(label, key=key)

# --- PREP SECTION // STARTING CHECKLIST ---
if nav_category == "Preparation":
    st.subheader("Preparation", divider="red")
    st.text("Overhead Panel:")

    cb_001 = persistent_checkbox("Turn On 'External Power'", key="cb_001")
    cb_002 = persistent_checkbox("Turn On 'Battery 1 & 2'", key="cb_002")
    cb_003 = persistent_checkbox("Set all (3) ADIRU to 'NAV'", key="cb_003")
    cb_004 = persistent_checkbox("Turn On 'Crew Supply'", key="cb_004")
    cb_005 = persistent_checkbox("Turn On 'APU Master' and wait 3 seconds", key="cb_005")
    cb_006 = persistent_checkbox("Turn On 'APU Start' and wait for 'APU AVAIL'", key="cb_006")
    cb_007 = persistent_checkbox("Turn On 'Nav Lights'", key="cb_007")
    cb_008 = persistent_checkbox("Turn 'Emergency Exit Lights' to ARM", key="cb_008")
    cb_009 = persistent_checkbox("Turn 'No Smoking Lights' to AUTO", key="cb_009")
    cb_010 = persistent_checkbox("Turn On all Fuel Pumps", key="cb_010")

Thanks a lot in advance! :slight_smile:

Best Regards,
Dome

The problem is coming from persistent_checkbox.
You are not really setting in the sesssion_state correctly.

    def persistent_checkbox(label, key):
        if 'checklist_items' not in st.session_state:
            st.session_state.checklist_items = {}
        state = st.checkbox(label, value=st.session_state.checklist_items.get(key, False), key=key)
        st.session_state.checklist_items[key] = state
        return state

    cb_001 = persistent_checkbox("Turn On 'External Power'", key="cb_001")
    cb_002 = persistent_checkbox("Turn On 'Battery 1 & 2'", key="cb_002")
    cb_003 = persistent_checkbox("Set all (3) ADIRU to 'NAV'", key="cb_003")
    cb_004 = persistent_checkbox("Turn On 'Crew Supply'", key="cb_004")

On the other page :

st.write(st.session_state.get('checklist_items, {})

Output :

{
"cb_001":true
"cb_002":true
"cb_003":true
"cb_004":false
}
2 Likes

Wow great!

@Faltawer
Changing the function for persistant_checkbox worked fine. Now I can switch the pages and the states will be saved. Thanks a lot for that!

I will take some time to analyze and understand your code.

When I push the button to reset the checkbox state - it will only reset the checkboxes in the “Navigation Selection”. Is it possible to reset them all with one click?

Have a nice day! :slight_smile:

Best Regards,
Dome

1 Like

If you use a button in this style :

if st.button('reset'):
    st.session_state['checklist_items']={}

It will erase all the items.
Otherwise, there is a more brutal approch :

for key in st.session_state.keys():
    del st.session_state[key]

It will remove all the session state.

:slight_smile:

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