Help needed: Restoring multiselect values after error occured

Hi, need some help. Pretty sure there is a solution for my problem, but I was not able to find something in the docs.

I have a multiselect with some preselected values (defaults). The multiselect is in a form and when I click on the save button some checks are performed and either the data is saved or rolled back. Internally the data is correctly handled but I cannot get the multiselect back to show the initial selection. Means, its not in sync anymore with my data.

Use Case: User removes an item from the multiselect and clicks on Save. The app detects that a user is not allowed to delete this item from the list, prints an error message and forces a rerun of streamlit.
What I want is that the multiselect shows the previous values

How do I achieve this?

Here is some a brief snippet of the code:

  my_options = ["A", "B", "C", "D"]
  my_defaults = ["B", "C"]
  key = "a"

    with st.form(key="metadata_subscriptions_form", clear_on_submit=False):
        if key not in st.session_state:
            st.session_state[key] = my_defaults

        selected = st.multiselect(
            f"Selections [{len(my_defaults)}]",
            options=my_options,
            default=st.session_state[key],
            key=key,
            label_visibility="visible"
        )
        if st.form_submit_button("Save Data", disabled=disabled):
            try:
                 # processing happens here
           except Exception as e:
                # Something went wrong. Want the values of the multiselect restored
                st.error(e)
                time.sleep(5)
                # Want my original selection back. How?
                # This does not work:
                # st.session state.a = my_defaults
                st.rerun()

The best way to reset a widget like this is to change the widget’s key. You can do that like this:

import streamlit as st
import time

my_options = ["A", "B", "C", "D"]
my_defaults = ["B", "C"]

if "key_suffix" not in st.session_state:
    st.session_state["key_suffix"] = 0

key = "a"

with st.form(key="metadata_subscriptions_form", clear_on_submit=False):
    selected = st.multiselect(
        f"Selections [{len(my_defaults)}]",
        options=my_options,
        default=my_defaults,
        key=f"{key}_{st.session_state['key_suffix']}", # USE KEY_SUFFIX TO UPDATE THE WIDGET KEY
        label_visibility="visible",
    )

    if st.form_submit_button("Save Data", disabled=False):
        try:
            # processing happens here
            raise ValueError("Testing")
        except Exception as e:
            # Something went wrong. Want the values of the multiselect restored
            st.error(e)
            time.sleep(1)
            st.session_state["key_suffix"] += 1 # UPDATE KEY_SUFFIX IF THERE'S AN ERROR
            # Want my original selection back. How?
            # This does not work:
            # st.session state.a = my_defaults
            st.rerun()

Thank you @blackary that works great!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.