18

我在 IPython 笔记本(作为 HTML 小部件的一部分)和 Spyre 应用程序(作为dropdown元素)中有一个下拉列表,比如说选择一个大陆,我想添加第二个下拉列表来选择大陆内的国家。现在显然第二个下拉列表中的选项取决于第一个下拉列表的值。我正在努力寻找一种方便的方法来拥有一个可以更新此 UI 元素的回调函数。

我几乎在 IPython 笔记本中完成了这项工作,其中我有一个interact函数,在被调用的函数中,我将使用第二interact个下拉菜单创建第二个元素。但是,每当我更改第一个下拉菜单时,都会创建一个新的下拉元素,因此每次更改都会产生一个额外的下拉菜单。但我只想更新一个下拉列表,仅此而已。

希望问题很清楚。谢谢你。

4

5 回答 5

22

使用interactive而不是interact更新您的小部件:

from IPython.html import widgets
from IPython.display import display

geo={'USA':['CHI','NYC'],'Russia':['MOW','LED']}

def print_city(city):
    print city

def select_city(country):
    cityW.options = geo[country]


scW = widgets.Select(options=geo.keys())
init = scW.value
cityW = widgets.Select(options=geo[init])
j = widgets.interactive(print_city, city=cityW)
i = widgets.interactive(select_city, country=scW)
display(i)
display(j)
于 2015-03-29T18:41:00.903 回答
6

投票最多的答案很有用,但对我来说似乎有点笨拙。搜索了一段时间后,我发现这里基于Jupyter docs的答案更适合我。我对它们进行了改编并提供了以下内容。

from ipywidgets import interact, Dropdown

geo = {'USA':['CHI','NYC'],'Russia':['MOW','LED']}
countryW = Dropdown(options = geo.keys())
cityW = Dropdown()

def update_cityW_options(*args): # *args represent zero (case here) or more arguments.
    cityW.options = geo[countryW.value]
cityW.observe(update_cityW_options) # Here is the trick, i.e. update cityW.options based on countryW.value.

@interact(country = countryW, city = cityW)
def print_city(country, city):
    print(country, city)

作为替代方案,我还发现我可以只更新cityW.optionsinsideprint_city函数,更清晰的做法!

from ipywidgets import interact, Dropdown

geo = {'USA':['CHI','NYC'],'Russia':['MOW','LED']}
countryW = Dropdown(options = geo.keys())
cityW = Dropdown()

@interact(country = countryW, city = cityW)
def print_city(country, city):
    cityW.options = geo[country] # Here is the trick, i.e. update cityW.options based on country, namely countryW.value.
    print(country, city)
于 2019-11-25T10:38:49.613 回答
4
import datetime
import ipywidgets as ipyw

from bokeh.models.widgets.inputs import AutocompleteInput
from IPython.display import display

dp1 = ipyw.Dropdown(options = ['Asia','Europe','Africa'])
dp2 = ipyw.Dropdown(options = ['India','China','Pakistan','Tibet'])
asia_list = ['India','China','Pakistan','Tibet']
europe_list = ['Germany','France','Italy']
africa_list = ['south africa','Nigeria','Kenya']
global_vbox = ipyw.VBox()
global_vbox.children = [dp1,dp2]
display(global_vbox)
def continent_change_event(x):
    global dp1
    global dp2
    list_name = dp1.value
    dp2.index = None #This line is very important for setting the values for widgets other than widget with observe method
    dp2.index = 0
    if(list_name == 'Asia'):
        dp2.options = asia_list
    elif(list_name == 'Europe'):
        dp2.options = europe_list
    else:
        dp2.options = africa_list

dp1.observe(continent_change_event)
于 2018-03-09T05:21:07.473 回答
2

对于那些使用widgets.interactive:更新主函数中动态小部件的选项:

import ipywidgets as widgets 
geo={'USA':['CHI','NYC'],'Russia':['MOW','LED']}

def main_function(city, country):
    print (f'{city} is a city in {country}')
    cityW.options = geo[country]    

scW = widgets.Select(options=geo.keys())
cityW = widgets.Select(options=geo[init])

widgets.interactive(main_function, city=cityW, country=scW)

注意:这只与评分最高的答案不同,只有一个交互功能而不是两个。奖励:如果您想将修复参数传递给您的主函数,请使用name=widgets.fixed("Bob")请参见此处)。

于 2020-02-12T09:16:27.573 回答
0

我拿了飞药溶液代替

cityW.observe(update_cityW_options)

为了

countryW.observe(update_cityW_options)

因为显示的城市应该取决于国家(我相信)。

也替换DropdownSelect(需要更少的点击)。

from ipywidgets import interact, Dropdown, Select

geo = {'USA':['CHI','NYC'],'Russia':['MOW','LED']}
countryW = Select(options = geo.keys())
cityW = Select()

def update_cityW_options(*args): # *args represent zero (case here) or more arguments.
    cityW.options = geo[countryW.value]

countryW.observe(update_cityW_options) # Here is the trick, i.e. update cityW.options based on countryW.value.
#---
@interact(country = countryW, city = cityW)
def print_city(country, city):
    print(country, city)

它看起来像这样。

在此处输入图像描述

#---除非您想打印所选组合(原始问题不要求),否则下面的部分是不必要的。

如果删除它,只需添加

# display the widgets
countryW
cityW 
于 2021-12-05T13:03:32.100 回答