Hi,
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,
default=a_default)
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.
Your page loads with nothing selected.
You select 1.
The page begins to rerun (st.session_state.a is still None).
When the line with the widget function is executed, it outputs the selection of 1.
The value of st.session_state.a is updated on the final line.
Now select 2.
The page begins to rerun (st.session_state.a contains just the selection of 1).
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,
default=st.session_state.a,
key='test'
)
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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.