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.
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")
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")
@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?