I couldn’t find in the forum a good way to download/upload all session_state widgets status, so I report here my approach hopefully somebody will find it useful, or someone else can help me improve the logic to make it more efficient.
Context: In my app, users set up some settings using widgets on the sidebar (to define perimeter of data + setting some rules & thresholds).
The same user will likely always use the same configuration, so it’s very useful for them to just upload their configuration file (downloaded from a previous run) containing all the widget status in a json format.
For this reason I’ve put a “Download Settings” button to download session state in a json file, a file uploader to upload the json file, and an “Apply Settings” button that updates the session state with the value contained in the json file previously downloaded.
Here the code snippet that you can use to see how it works:
import streamlit as st
import json
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()
# Create all the widgets
st.subheader("DEFINE ALL THE SETTINGS")
checkbox_1 = st.checkbox(label="Checkbox 1", key="checkbox_1")
radio_1 = st.radio(label="Radio 1", options=["a", "b", "c"], key="radio_1")
slider_1 = st.slider(label="Slider 1", min_value=0, max_value=10, key="slider_1")
multiselect_1 = st.multiselect(label="Multiselect 1", options=["A", "B", "C"], key="multiselect_1")
text_input_1 = st.text_input(label="Text Input 1", key="text_input_1")
button_1 = st.button(label="Button 1")
# Run the download_upload_settings function
with container_upload_settings_data:
with st.expander(label="UPLOAD CUSTOM SETTINGS / DATA", expanded=False):
download_upload_settings()
Let me know if you have feedbacks or improvement recommendations
Thanks
Federico