-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In selectbox, if options chage from None to not, the defalut value rendered is still None. #10093
Comments
Can you provide a code snippet and steps to reproduce the problem? Since the value in Session State and the output of the widget is the option value and not the index, I can't generate an error from this. I tried a simple selectbox with a import streamlit as st
a = st.selectbox("A", [1, 2, None], index=None, key="key")
st.session_state.key
a |
here is the code:
with this kind of situation, index will never work after project from 1 to 2. The expected rendering should be that when 'project2' is selected, the dataset selectbox should default to 'dataset1', because index=0 if len(dataset_option) > 0 else None. However, if you run the code, you will find that with project changes, dataset is always defalut to be None. |
this problem will happen in v1.40.0 and v1.41.0. but in 1.33.0, it works as expected |
That makes sense. The ability to set the initial value to import streamlit as st
st.write(st.session_state)
st.button("Rerun")
project = st.selectbox("Select the project", options=[1, 2, 3], key="project_selection")
if project == 1:
dataset_option = []
elif project == 2:
dataset_option = ["dataset1", "dataset2"]
if project != 3:
dataset = st.selectbox("Select the dataset", options=dataset_option, key="dataset_selection", index=0 if len(dataset_option) > 0 else None)
if dataset is None:
st.error("No datasets available.")
st.write(st.session_state)
|
Apologies for the confusion. I wasn't suggesting a solution; I was trying to assist the devs for when they get back from holiday break so they can jump right to marking this as confirmed and prioritizing the fix. 🙂 |
If you want a workaround for this until it's fixed, clearing the key from Session State would work: import streamlit as st
def clear_key():
del st.session_state.dataset_selection
project = st.selectbox("Select the project", options=[1, 2], key="project_selection", on_change=clear_key)
if project == 1:
dataset_option = []
elif project == 2:
dataset_option = ["dataset1", "dataset2"]
dataset = st.selectbox("Select the dataset", options=dataset_option, key="dataset_selection", index=0 if len(dataset_option) > 0 else None)
if dataset is None:
st.error("No datasets available.") |
The |
I get that. And will this be
Will this be fixed in the feature? 😂 |
I'll defer to the devs when they get back from holiday break. 😅 My guess is: probably. It is the case in general that if you have a widget with |
Hi @vivianyang11 , Thank you for filing this issue! I've confirmed it and triaged the issue for fixing. Thank you @sfc-gh-dmatthews for providing a workaround. Laura |
@sfc-gh-lwilby I noticed it also happens with |
Checklist
Summary
in the selectbox code, line 313-314, it shows that if last time, the selected item is None, change the index to None. This will cause a problem that if options chage from None to not, the defalut value rendered is still None.
Reproducible Code Example
No response
Steps To Reproduce
No response
Expected Behavior
No response
Current Behavior
No response
Is this a regression?
Debug info
Additional Information
No response
The text was updated successfully, but these errors were encountered: