Skip to content

st.selectbox UI unexpectedly switches to first option #13435

@JosephMarinier

Description

@JosephMarinier

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

When setting the value of a st.selectbox via the session state API (to something else than the first option) and interacting with the selectbox (opening it and closing it without selecting an option), the selectbox visually switches to the first option (even though the backend keeps returning the value originally set via the session state API).

Reproducible Code Example

import streamlit as st

with st.container(horizontal=True):
    for value in (1, 2, 3):
        if st.button(f"Set {value}"):
            st.session_state["key"] = value
selected_option = st.selectbox("Select", options=(1, 2, 3), index=0, key="key")
st.text(f"Selected option: {selected_option}")

Steps To Reproduce

  1. Try the above code example.
  2. Click on Set 2.
  3. Click on the selectbox.
  4. Click outside the selectbox.

Expected Behavior

The selectbox should keep displaying 2.

Current Behavior

The selectbox displays 1 (the first option).

Additionally, the text below still displays Selected option: 2, exacerbating the discrepancy between the frontend and the backend. This is the value returned by st.selectbox or the st.session_state["key"].

Image

You can reproduce it again if you click on Set 3, click on the selectbox, and click outside the selectbox. Once you select an option in the selectbox, the bug won't happen for the duration of the session. Refresh the page (new session) to reproduce the bug again.

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.52.2
  • Python version: 3.11.9
  • Operating System: MacOS
  • Browser: Chrome

Additional Information

This bug does NOT occur if the index argument is provided to st.selectbox, but...
If I set index=None, the selectbox becomes clearable, and I don't want that in my use case.

if "key" not in st.session_state:
    st.session_state["key"] = 2
selected_option = st.selectbox("Select", options=(1, 2, 3), index=None, key="key")
st.text(f"Selected option: {selected_option}")

If I set index=1 (or any other integer greater than 0), the selectbox behaves perfectly, but it shows the following warning.

if "key" not in st.session_state:
    st.session_state["key"] = 2
selected_option = st.selectbox("Select", options=(1, 2, 3), index=1, key="key")
st.text(f"Selected option: {selected_option}")
Image

Metadata

Metadata

Assignees

Labels

feature:st.selectboxRelated to the `st.selectbox` widgetfeature:st.session_stateRelated to session state managementpriority:P2Medium-high priority - fix within one monthstatus:confirmedBug has been confirmed by the Streamlit teamtype:bugSomething isn't working as expected

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions