Hey there !
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")