st.selectbox - No selection per default
See original GitHub issueProblem
I believe it is currently not possible to use a selectbox that has nothing selected per default. This may be undesirable in some cases.
Solution
Replace the index
parameter with a default
parameter just like the multiselect item does. Problem: This is a breaking change.
Possibly better: Instead of throwing an error when the provided index is not within the list, return None
and show an empty selection in the UI instead of throwing an error. Problem: No explicit error is being raised in case the index out of bound in an unwanted scenario.
Possibly possibly better: An index of -1
indicates an empty selection and None
is returned. This might be a good balance since negative values are currently forbidden.
Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.
If you’d like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:28
- Comments:6 (2 by maintainers)
Top GitHub Comments
I would find it very useful to be able to have an selection box without a default value selected!
A clean solution would probably need to have a dedicated default value that cannot be mistaken for any actual value ( `` could possibly be a valid option to select). I didn’t think of the
format_func
to actually format this exceptional value and thus had to use a string, but it’s a very good point.For reference, you’ll find below the wrapper I wrote in December to handle a very similar use case.
I’d then call it from the
app.py
code with