亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

當另一個選擇小部件更改時,如何自動更新下拉選擇小部件?(Python面板pyviz)

當另一個選擇小部件更改時,如何自動更新下拉選擇小部件?(Python面板pyviz)

一只甜甜圈 2022-05-24 16:22:34
我有一個 Select 小部件,每當另一個 Select 小部件更改時,它應該提供不同的選項列表,因此只要另一個 Select 小部件更改,它就會更新。我如何在下面的示例代碼中做到這一點? _countries = {    'Africa': ['Ghana', 'Togo', 'South Africa'],    'Asia'  : ['China', 'Thailand', 'Japan'],    'Europe': ['Austria', 'Bulgaria', 'Greece']}continent = pn.widgets.Select(    value='Asia',     options=['Africa', 'Asia', 'Europe'])country = pn.widgets.Select(    value=_countries[continent.value][0],     options=_countries[continent.value])@pn.depends(continent.param.value)def _update_countries(continent):    countries = _countries[continent]    country.options = countries    country.value = countries[0]pn.Row(continent, country)
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

所以,我花了很長時間才發現這一點,但是在你的 @pn.depends() 中你必須添加參數 watch=True,所以它會不斷地監聽是否發生了變化,并且應該更新你的其他列表。

在這種情況下:


@pn.depends(continent.param.value, watch=True)

整個例子:


_countries = {

    'Africa': ['Ghana', 'Togo', 'South Africa'],

    'Asia'  : ['China', 'Thailand', 'Japan'],

    'Europe': ['Austria', 'Bulgaria', 'Greece']

}


continent = pn.widgets.Select(

    value='Asia', 

    options=['Africa', 'Asia', 'Europe']

)


country = pn.widgets.Select(

    value=_countries[continent.value][0], 

    options=_countries[continent.value]

)


@pn.depends(continent.param.value, watch=True)

def _update_countries(continent):

    countries = _countries[continent]

    country.options = countries

    country.value = countries[0]


pn.Row(continent, country)

此頁面上的 GoogleMapViewer 示例為我指明了正確的方向:

Selector updates after another selector is changed


相同的答案,但隨后以類的形式出現:


class GoogleMapViewer(param.Parameterized):


    continent = param.Selector(default='Asia', objects=['Africa', 'Asia', 'Europe'])


    country = param.Selector(default='China', objects=['China', 'Thailand', 'Japan'])


    _countries = {'Africa': ['Ghana', 'Togo', 'South Africa'],

                  'Asia'  : ['China', 'Thailand', 'Japan'],

                  'Europe': ['Austria', 'Bulgaria', 'Greece']}


    @param.depends('continent', watch=True)

    def _update_countries(self):

        countries = self._countries[self.continent]

        self.param['country'].objects = countries

        self.country = countries[0]


viewer = GoogleMapViewer(name='Google Map Viewer')

pn.Row(viewer.param)


查看完整回答
反對 回復 2022-05-24
  • 1 回答
  • 0 關注
  • 84 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號