Load different multiselect default values


imagine iโ€™m saving different scenarios using firestore (or whatever).
This scenarios have pre-selected default values for widget multiselect, that i want to display.
What shall i do to achieve this?

I tried following code

Code snippet:

import streamlit as st

if 'a' not in st.session_state.keys():
    st.session_state['a'] = None

a_default = st.session_state['a']
a = [1,2,34,35]
a = st.multiselect('Test a', a,
st.session_state['a'] = a

Expected behavior:

I load my default values, store it in st.session_state, then i display it in multiSelect. Also, i can edit those values with the same widget.

Actual behavior:

I need to select 2 times a new value in the widget (same for de-select).
First time, just ignores it, and keeps the default values given.

When you start to dynamically update widgets, you will encounter an specific detail about widgets: If you change any of their defining parameters, the widget will reset (get thrown away and recreated from scratch with the new parameters). This will happen if you change the label, options, or default value of st.multiselect for example.

  1. Your page loads with nothing selected.
  2. You select 1.
  3. The page begins to rerun (st.session_state.a is still None).
  4. When the line with the widget function is executed, it outputs the selection of 1.
  5. The value of st.session_state.a is updated on the final line.
  6. Now select 2.
  7. The page begins to rerun (st.session_state.a contains just the selection of 1).
  8. When the line with the widget function is executed, the previous widget is thrown away (that had a default value of None) and a new widget is created (that has a default value of [1]). The fact that 2 was selected is discarded along with the original widget.

There is a widget guide to explain this in detail and how to work with updating widgets. An example is included at the end, but I recommend reading and understanding the concepts above it.

Thanks for the explanation.
In case, anyone else has a similar issue.

After reading the docs, i understand that using output of the widget as dynamic default values for same widget, drives the issue.

To by pass this, and avoid st inconsistent behavior, i add a form to make the dynamic value storing conditional.

import streamlit as st

if 'a' not in st.session_state.keys():
    st.session_state['a'] = None

a_default = st.session_state.a

a = [1,2,34,35]
a = st.multiselect('Test a', a,
with st.form('submit a'):
    st.session_state['a'] = a
    st.form_submit_button('Submit a')

A form will only delay the value of widgets. A direct assignment to Session State within a form will still happen on every script run. If you first change something in a widget and then submit the form, youโ€™ve run the script twice. This just pushes the โ€œselect two timesโ€ issues out a bit to โ€œselect then click a button.โ€ In principle, adding an extra rerun to your script is one solution. You can do this programmatically with st.rerun to avoid the user still clicking twice to achieve the result.

Alternatively, here is a possible solution (which will be compatible with a multipage app).

import streamlit as st

# Initialize your value with the desired default.
# "a" will be the permanent key to reference the selections.
if "a" not in st.session_state:
    st.session_state["a"] = []

def update():
    # Update permanent key-value pair from temporary one.
    st.session_state.a = st.session_state.temp_a

# Get value for widget from permanent key.
st.session_state.temp_a = st.session_state.a

a_options = [1,2,34,35]
st.multiselect("Widget a", a_options, key="temp_a", on_change=update)

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