Jupyter笔记本上的多个依赖小部件(下拉菜单)

4
我正在按照这里的教程学习在Jupyter Notebook中如何处理多个依赖小部件的示例:动态更改IPython笔记本小部件和Spyre中的下拉菜单。在该示例中,代码解决方案如下:
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.Dropdown(options=geo.keys())
init = scW.value
cityW = widgets.Dropdown(options=geo[init])
j = widgets.interactive(print_city, city=cityW)
i = widgets.interactive(select_city, country=scW)
display(i)
display(j)

所以,第二个下拉菜单的值取决于第一个下拉菜单的值。这里有一个问题:如果我想创建一个第三个下拉菜单,它依赖于第二个下拉菜单的值怎么办?假设上面的每个城市(CHI、NYC、MOW、LED)都有一些区域,我希望一个第三个下拉菜单随着城市更新而更改。希望问题清楚明了,谢谢!

https://stackoverflow.com/questions/62258847/using-multiple-depended-widgets-from-jupyter-notebook-and-interactive-problem - Eichstier
嘿,你能看一下这个吗?https://stackoverflow.com/questions/62258847/using-multiple-depended-widgets-from-jupyter-notebook-and-interactive-problem 我可能需要一些帮助。 - Eichstier
2个回答

3

如果你还没找到解决方案,我这里有一个在Python 3中可用的解决方案。我已经注释了我从原始代码中更改的所有内容。希望能对你有所帮助!

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

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

geo2={'CHI':['1','2'],'NYC':['3','4'],'MOW':['5','6'],'LED':['7','8']} #create dictionary with city districts

def print_city(city,district):
    print(city)
    print(district) #add in command to print district

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

#add in 'select district' function that looks in the new dictionary
def select_district(city):
    districtW.options = geo2[city]

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


init2= cityW.value #new start value for district dropdown
districtW = widgets.Dropdown(options=geo2[init2]) #define district dropdown widget

j = widgets.interactive(print_city, city=cityW, district=districtW) #define district value
i = widgets.interactive(select_city, country=scW)

k = widgets.interactive(select_district, city=cityW) #call everything together with new interactive

display(i)
display(j)

3

对我来说,使用interactive有点笨拙。我在原始页面这里为两个相关小部件提供了更清晰和更简洁的答案。下面是我为多个相关小部件提供的答案。

from ipywidgets import interact, Dropdown

geo = {'USA':['CHI','NYC'],'Russia':['MOW','LED']}
geo2={'CHI':['1','2'],'NYC':['3','4'],'MOW':['5','6'],'LED':['7','8']}

countryW = Dropdown(options = geo.keys())
cityW = Dropdown(options = geo[countryW.value]) # options = geo[countryW.value] is to remove inital error but not that necessary.
districtW = Dropdown()

@interact(country = countryW, city = cityW, district = districtW)
def print_city(country, city, district):
    cityW.options = geo[country] # Here is the trick, i.e. update cityW.options based on country, namely countryW.value.
    districtW.options = geo2[city] # Dittoo
    print(country, city, district)

另一种方法是使用显式的更新函数,如下所示。请记住,更新速度可能不那么快,但代码运行良好。

from ipywidgets import interact, Dropdown

geo = {'USA':['CHI','NYC'],'Russia':['MOW','LED']}
geo2={'CHI':['1','2'],'NYC':['3','4'],'MOW':['5','6'],'LED':['7','8']}

countryW = Dropdown(options = geo.keys())
cityW = Dropdown()
districtW = 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.

def update_districtW_options(*args):
    districtW.options = geo2[cityW.value]
districtW.observe(update_districtW_options)

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

非常好的答案。运行良好。然而,有时我会看到轴被重复两次。第一张图是我们想要的图表,第二张和第三张是轴被复制的图表。有什么办法可以忽略它们吗? - prashanth
@prashanth 感谢您的评论。是的,如果我们更改2/1级别的值,我们将获得1/2个重复的项目。很抱歉我目前没有解决方案。一旦我解决了这个问题,我会在这里回复。 - Fei Yao
当然。谢谢。 - prashanth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接