设置选择框的默认值

问题描述 投票:0回答:4

我是streamlit新手。 我尝试为 sidebar.selectbox 设置默认值。 代码如下。 我很感激你的帮助! 预先感谢您。

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
python streamlit
4个回答
24
投票

使用

index
小部件的
selectbox
关键字。传递
options
列表中您想要作为默认选择的值的索引。

例如如果您想将标记为

'Window ANTICOR'
的选择框的默认选项设置为 30(您似乎正在尝试这样做),您可以简单地执行以下操作:

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

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


2
投票

您也可以直接给出默认位置的索引

bins = st.sidebar.radio(标签=“类别:”,选项=n3,索引=0) 欲了解更多信息 在此输入链接描述


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)

0
投票

您可以使用

index
,这在
st.sidebar.selectbox
st.selectbox
中都有效(演示):

"""
setting a default value for a selectbox
"""
import streamlit as st

options = ["Apple", "Banana", "Cherry", "Date", "Fig", "Grape", "Pear"]
default_index = options.index("Apple")

# works in a sidebar...
st.sidebar.selectbox('Pick a fruit', options, index=default_index)

# and a standalone selectbox
st.selectbox('Pick a fruit', options, index=default_index, key='select-box')
© www.soinside.com 2019 - 2024. All rights reserved.