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()