Fill CheckboxColumn from session_state download

I have a local app where a user can tick a checkbox column in a table.

What I want is that the user can download the checked rows and upload it in the app at another session.

What I have so far is:

  • multiselect to select on or more years
  • table(name, surname, age) with CheckboxColumn
  • add the checked rows and the selected years in a session_state
  • download and upload (and apply) mechanism for the session_state

But what is not working, is that when the users uploads an applies the session_state file, is that the rows in the session_state are ticked automatically in the in the checkboxtable.
The years (from the multiselect) are updated fine!

order of the app:

  1. select 1 or more years (choose years)
  2. check 1 or more boxes in the Select column
  3. download settings in sidebar
  4. optional: change the multiselect/Select checkbox
  5. upload the downloaded settings in sidebar
  6. apply settings button in sidebar

result is an error message:

StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, and st.form cannot be set using st.session_state.

Traceback:
File “C:_NoBackup\Workspace\python\test\ss\main.py”, line 28, in
ct = st.data_editor(

streamlit version: 1.32.0
python version: 3.12.3

streamlit code:

import streamlit as st
import pandas as pd
import json

#1: years with multiselect is working with upload session_state file
years = pd.DataFrame(
{‘years’: [ ‘2023’,‘2024’,‘2025’]
}
)
st.multiselect('choose years: ', years, key = ‘years_selected’)
years_selected = st.session_state[‘years_selected’]

#2 table with checkboxcolumn is not working with upload session_state file
#create dataset
data = pd.DataFrame(
{‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’, ‘David’],
‘Surname’: [‘Smith’, ‘Johnson’, ‘Williams’, ‘Brown’],
‘Age’: [25, 30, 35, 40 ]
}
)

#insert checkbox column
data.insert(0, “Select”, False)

#create checkbox table
ct = st.data_editor(
data,
column_config={
“Select”: st.column_config.CheckboxColumn(“Select”,
default=False
)
}
,disabled=[‘Name’, ‘Surname’, ‘Age’]
,hide_index = True
,key = ‘checked_rows’
)

st.markdown(“session state:”)
st.write(st.session_state)

#DOWNLOAD / UPLOAD SETTINGS

def download_upload_settings():
# 1. Download Settings Button
col1, col2 = st.columns([6, 5])
settings_to_download = {k: v for k, v in st.session_state.items()
if “button” not in k and “file_uploader” not in k}
button_download = col1.download_button(label=“Download Settings”,
data=json.dumps(settings_to_download),
file_name=f"settings.json",
help=“Click to Download Current Settings”)

# 2. Select Settings to be uploaded
uploaded_file = st.file_uploader(label="Select the Settings File to be uploaded",
                                 help="Select the Settings File (Downloaded in a previous run) that you want"
                                      " to be uploaded and then applied (by clicking 'Apply Settings' above)"
                                      " in order to filter the perimeter")
if uploaded_file is not None:
    uploaded_settings = json.load(uploaded_file)
else:
    uploaded_settings = settings_to_download
    st.warning("**WARNING**: Select the Settings File to be uploaded")

# 3. Apply Settings
def upload_json_settings(json_settings):
    """Set session state values to what specified in the json_settings."""
    for k in json_settings.keys():
        st.session_state[k] = json_settings[k]
    return

button_apply_settings = col2.button(label="Apply Settings",
                                    on_click=upload_json_settings,
                                    args=(uploaded_settings,),
                                    help="Click to Apply the Settings of the Uploaded file.\\\n"
                                         "Please start by uploading a Settings File below")

with st.sidebar:
# Create a container to put the download/upload settings at the top
container_upload_settings_data = st.container()
with container_upload_settings_data:
with st.expander(label=“UPLOAD CUSTOM SETTINGS / DATA”, expanded=True):
download_upload_settings()
st.write('upload/download source url: ', ‘Download & Upload Session State’)

Hi @Alexander_van_Roij

I think that st.data_editor could be used to accept user input on the rows to select, which will allow you to obtain the selected rows from users. When users upload their settings, there should be a validation logic in place to ensure that there is a match between user provided selections and the DataFrame from which a data subset is extracted from.

The error seems to arise from st.form cannot be set using st.session_state.. Can you ensure that the appropriate slice of the session state variable is made and not the entirety of the session state variable. For example, st.session_state['var1'] or st.session_state['var1'][0], etc.

Hope this helps!

Hi @dataprofessor : I have tried to use only the checked column, but when uploading and applying the settings, the checked boxes are not filled from the session state.