Set default value for selectbox
Asked Answered
B

3

16

I am new to streamlit. I tried to set a default value for sidebar.selectbox. The code is below. I appreciate the help! Thank you in advance.

st.sidebar.header('Settings')

fichier = st.sidebar.selectbox('Dataset', ('djia', 'msci', 'nyse_n', 'nyse_o', 'sp500', 'tse'))

window_ANTICOR = st.sidebar.selectbox('Window ANTICOR', ['<select>',3, 5, 10, 15, 20, 30])
if window_ANTICOR == '<select>':    
    window_ANTICOR == 30

window_OLMAR = st.sidebar.selectbox('Window OLMAR', ['<select>',3, 5, 10, 15, 20, 30])
if window_OLMAR == '<select>':    
    window_OLMAR == 5

eps_OLMAR = st.sidebar.selectbox('Eps OLMAR', ['<select>', 3, 5, 10, 15, 20, 30])
if eps_OLMAR == '<select>':    
    eps_OLMAR == 10

eps_PAMR = st.sidebar.selectbox('Eps PAMR', ['<select>',0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
if eps_PAMR == '<select>':    
    eps_PAMR == 0.5

variant = st.sidebar.selectbox('Variant PAMR', (0, 1, 2))
if variant == '<select>':    
    eps_PAMR == 0
Birdwell answered 26/11, 2020 at 17:35 Comment(0)
B
24

Use the index keyword of the selectbox widget. Pass the index of the value in the options list that you want to be the default choice.

E.g. if you wanted to set the default choice of the selectbox labeled 'Window ANTICOR' to 30 (which you appear to be trying to do), you could simply do this:

values = ['<select>',3, 5, 10, 15, 20, 30]
default_ix = values.index(30)
window_ANTICOR = st.sidebar.selectbox('Window ANTICOR', values, index=default_ix)

Source: https://docs.streamlit.io/library/api-reference/widgets/st.selectbox

Bullis answered 4/12, 2020 at 14:30 Comment(0)
N
2

you can directly give index also for default position

bins = st.sidebar.radio(label="Category :", options=n3, index=0) for more information enter link description here

Nihility answered 29/8, 2022 at 7:44 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewChinese
L
1
values = ['Select', 10, 15, 20, 25, 30]
default_ix = values.index(10)
if values == 'Select':
    st.warning("Choose the integers from the list in the dropdown")
else:
    components = st.selectbox("Select the components below⤵️", values, 
    index=default_ix)
Lintwhite answered 27/7, 2023 at 2:24 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Koball

© 2022 - 2025 — McMap. All rights reserved.