Save checkbox selection session state

Hey there :grin:!
I am currently working on a webpage where users can go through a list and select with checkboxes whether a particular point is relevant to them or not. Unfortunately, I am unable to save the checkbox selections in the session_state, so the selections remain visible even after a page refresh. Could you help me with this?

def update_state_generic(key_prefix, gruppe, unterthema, auswahl):
   key = f"{key_prefix}_{gruppe}_{unterthema}"
   st.session_state[key] = auswahl

def create_expander(thema, unterthemen, state_key):
  with st.expander(thema):
    col_text, col_rb1, col_rb2, col_rb3 = st.columns([6, 1, 1, 1])
    if thema == unterthemen_esrse1:
        col_text.write("**" + "Klimawandel" + "**")
    elif thema == unterthemen_esrse2:
        col_text.write("**" + "Umweltverschmutzung" + "**")
    elif thema == unterthemen_esrse3:
        col_text.write("**" + "Wasser- & Meeresressourcen" + "**")
    elif thema == unterthemen_esrse5:
        col_text.write("**" + "Kreislaufwirtschaft" + "**")
    elif thema == unterthemen_esrsg1:
        col_text.write("**" + "Unternehmenspolitik" + "**")

    col_rb1.write("Wesenlich")
    col_rb2.write("Teilweise Wesentlich")
    col_rb3.write("Nicht Wesentlich")
    for unterthema in unterthemen:
        col_text, col_rb1, col_rb2, col_rb3 = st.columns([6, 1, 1, 1])
        with col_text:
            st.markdown(f"<p style='font-family:Source Sans Pro;'>β€’ {unterthema}</p>", unsafe_allow_html=True)
        for i, option in enumerate(optionen):
            col = [col_rb1, col_rb2, col_rb3][i]
            with col:
                checkbox_key = f"{state_key}_{unterthema}_{option}"
                checkbox_value = st.session_state.get(checkbox_key, False)
                # Hier passen wir den Aufruf an die erwartete Signatur von update_state_generic an
                st.checkbox("Select", key=checkbox_key, value=checkbox_value, on_change=update_state_generic, args=(state_key, '', unterthema, option), label_visibility="collapsed")
1 Like

Perhaps you can handle the session status updates separately, perhaps by adding a submit button or other mechanism to trigger the updates.

import streamlit as st

# Define your data
themen = ["Thema 1", "Thema 2", "Thema 3"]
unterthemen_esrse1 = ["Unterthema 1", "Unterthema 2", "Unterthema 3"]
optionen = ["Wesenlich", "Teilweise Wesentlich", "Nicht Wesentlich"]

# Function to update session state
def update_state_generic(key_prefix, gruppe, unterthema, option, auswahl):
    key = f"{key_prefix}_{gruppe}_{unterthema}_{option}"
    st.session_state[key] = auswahl

# Function to create expander
def create_expander(thema, unterthemen, state_key):
    with st.expander(thema):
        for unterthema in unterthemen:
            st.write(f"### {unterthema}")
            for option in optionen:
                checkbox_key = f"{state_key}_{unterthema}_{option}"
                checkbox_value = st.session_state.get(checkbox_key, False)
                checkbox_value = st.checkbox(option, key=checkbox_key, value=checkbox_value)
                update_state_generic(state_key, '', unterthema, option, checkbox_value)

# Usage
create_expander("ESR/SE", unterthemen_esrse1, "esrse")

# code to send the information .....

@AndreeS98 did you find any solution for preserving the row state, except for having the new button?

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