question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

st.selectbox - No selection per default

See original GitHub issue

Problem

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 indexparameter 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:open
  • Created 4 years ago
  • Reactions:28
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
EwoutHcommented, Dec 30, 2021

I would find it very useful to be able to have an selection box without a default value selected!

8reactions
nicolasdaviaudcommented, Apr 15, 2020

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.

DEFAULT = '< PICK A VALUE >'

def selectbox_with_default(text, values, default=DEFAULT, sidebar=False):
    func = st.sidebar.selectbox if sidebar else st.selectbox
    return func(text, np.insert(np.array(values, object), 0, default))

I’d then call it from the app.py code with

res = selectbox_with_default('some title', some_values)
if res == DEFAULT:
    st.warning("Please fill all the fields !")
    raise StopException

Read more comments on GitHub >

github_iconTop Results From Across the Web

st.selectbox - Streamlit Docs
Default is "visible". This argument can only be supplied by keyword. Returns. (any). The selected option ...
Read more >
Set default value for selectbox - python
Use the index keyword of the selectbox widget. Pass the index of the value in the options list that you want to be...
Read more >
How to use the streamlit.selectbox function in streamlit
To help you get started, we've selected a few streamlit.selectbox examples, based on popular ways it is used in public projects.
Read more >
select-options default values
hi guys, in the selection screen how to fill default values for ... an entry specifying that cities not beginning with " SAN...
Read more >
Add Default Choices
To access piped text in a default choice, edit your default choices and click the blue dropdown menu directly next to any text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found