第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動態(tài)更改 Streamlit.multiselectbox 選項

動態(tài)更改 Streamlit.multiselectbox 選項

Smart貓小萌 2023-12-09 15:34:59
我的 Streamlit 應(yīng)用程序的側(cè)邊欄中有一個多選框。不失一般性:我有 10 個選項可以放入這個多選框中(這些是數(shù)字 1...10)用戶不能同時選擇1兩者2因此,我想2從可能的選擇列表中刪除 if 1,但 Streamlit 似乎沒有此功能,因此我嘗試將其包裝在循環(huán)中,這也失敗了(DuplicateWidgetID: There are multiple identical st.multiselect widgets with the same generated key.):options = [1,2,3,4,5,6,7,8,9,10]space = st.sidebar.empty()answer, _answer = [], Nonewhile True:    if answer != _answer:        answer.append(space.multiselectbox("Pick a number",                                           options,                                           default=answer                                           )                      )        options = [o for o in options if o not in answer]        if 1 in options:            if 2 in options: options.remove(2)        if 2 in options:            if 1 in options: options.remove(1)        _answer = answer[:]關(guān)于我如何實現(xiàn)這一目標(biāo)有什么想法嗎?
查看完整描述

1 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗 獲得超6個贊

方法一

這是一種方法,在小部件上提供幫助,只允許用戶選擇 1 和 2,但如果兩者都被選擇,我們必須過濾掉 2。然后您就可以使用經(jīng)過驗證的選擇。


代碼

import streamlit as st


ms = st.sidebar.multiselect('Pick a number',  list(range(1, 11)),

    help='Choose either 1 or 2 but not both. If both are selected 1 will be used.')


if 1 in ms and 2 in ms:

    ms.remove(2)


st.write('##### Valid Selection')

st.write(str(ms))

輸出

將鼠標(biāo)懸停在 上?可顯示幫助。

https://img1.sycdn.imooc.com/657418c200017a9d04940188.jpg

方法二

使用單選按鈕選擇選項 1 或 2,其余選項用于多選。


代碼

import streamlit as st


rb = st.sidebar.radio('Pick a number', [1, 2])


ms = st.sidebar.multiselect('Pick a number',  list(range(3, 11)))


selected = ms

selected.append(rb)


st.write('##### Valid Selection')

st.write(str(selected))

輸出

https://img1.sycdn.imooc.com/657418cd00011a8d04890283.jpg

方法三

選擇 1 后,刪除 2,然后重新運(yùn)行以更新選項。同樣,當(dāng)選擇 2 時,刪除 1 并重新運(yùn)行以更新選項。


代碼

import streamlit as st



init_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



if 'options' not in st.session_state:

    st.session_state.options = init_options

if 'default' not in st.session_state:

    st.session_state.default = []



ms = st.sidebar.multiselect(

    label='Pick a number',

    options=st.session_state.options,

    default=st.session_state.default

)


# If 1 is selected, remove the 2 and rerun.

if 1 in ms:

    if 2 in st.session_state.options:

        st.session_state.options.remove(2)

        st.session_state.default = ms

        st.experimental_rerun()


# Else if 2 is selected, remove the 1 and rerun.

elif 2 in ms:

    if 1 in st.session_state.options:

        st.session_state.options.remove(1)

        st.session_state.default = ms

        st.experimental_rerun()



st.write('##### Valid Selection')

st.write(str(ms))

輸出

https://img1.sycdn.imooc.com/657418da000146af05140493.jpg

查看完整回答
反對 回復(fù) 2023-12-09
  • 1 回答
  • 0 關(guān)注
  • 343 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號