Need all checkbox title from st.expander

Dear Boss

My code is functioning correctly within this application. I have two st.expander components, and I would like to retrieve the checkbox titles from the second st.expander, which is labeled ‘Excluded’. Please provide me with the correct code to obtain all the checkbox titles from the second expander.

My Code

import streamlit as st
import pandas as pd
def initialize():
if ‘df’ not in st.session_state:
data = {}
for i in range(20):
col = f’col{i}’
data[col]= range(10)
st.write(‘initializing’)
df = pd.DataFrame(data)
st.session_state.df = df
st.session_state.columns = list(df.columns)

initialize()
df = st.session_state.df
columns = st.session_state.columns
def move_column(col, state):
if state:
st.session_state[col] = True
st.session_state.columns.remove(col)
else:
st.session_state[col] = False
st.session_state.columns.append(col)

configure = st.columns(2)
with configure[0]:
included = st.expander(‘Included’, expanded=True)
with included:
st.write(‘’)
with configure[1]:

excluded = st.expander(‘Excluded’, expanded=True)
with excluded:
st.write(‘’)

for col in df.columns:
if col in st.session_state.columns:
with included:
st.checkbox(col,key=col, value=False, on_change=move_column, args=(col,True))
else:
with excluded:
st.checkbox(col, key=col, value=True, on_change=move_column, args=(col,False))
st.write(col)
df[columns]
st.markdown(‘’'[data-testid=“stExpander”] ul [data-testid=“stVerticalBlock”]

{overflow-y:scroll; max-height:400px;} ‘’', unsafe_allow_html=True)

thank you in advance for your help

regard

Welcome to the community and thanks for your detailed question! :blush: To get all the checkbox titles from the second st.expander (“Excluded”), you just need to collect the column names that are not in st.session_state.columns. Here’s how you can do it:

Add this line after your for loop (where you create the checkboxes):

excluded_titles = [col for col in df.columns if col not in st.session_state.columns]
st.write("Checkbox titles in 'Excluded':", excluded_titles)

This will display a list of all checkbox titles currently in the “Excluded” expander. For more context and similar UI logic, see the discussion at Streamlit forum: column selection in the dashboard.

Sources: