Download & Upload Session State

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",
                                           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)
        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]

    button_apply_settings = col2.button(label="Apply 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 ="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):

Let me know if you have feedbacks or improvement recommendations


Great idea and execution! Love it!

This is just what I was looking for! Thank you so much for sharing :smiley: