【问题标题】:Multiple dependent widgets (dropdown menu) on Jupyter notebookJupyter notebook 上的多个依赖小部件(下拉菜单)
【发布时间】:2018-07-02 14:06:49
【问题描述】:

我正在关注如何从这里处理 jupyter 笔记本上的多个依赖小部件的示例:

Dynamically changing dropdowns in IPython notebook widgets and 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)都有一些区,我想要第三个下拉菜单,每次更新城市时都会改变。

希望问题清楚,谢谢!

【问题讨论】:

标签: python python-3.x drop-down-menu widget jupyter-notebook


【解决方案1】:

以防万一您从未找到解决方案:我这里有一个适用于 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)

【讨论】:

    【解决方案2】:

    使用interactive 对我来说有点笨拙。我已经为原始页面here 中的两个依赖小部件提供了更清晰和简洁的答案。下面我为多个依赖的小部件提供我的答案。

    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)
    

    【讨论】:

    • 很好的答案。工作得很好。但是,有时我会看到轴重复了 2 次。第一个图是我们想要的图,第二个和第三个是复制轴的图。关于如何忽略它们的任何想法?
    • @prashanth 感谢您的评论。是的,您是对的,如果我们更改 2/1 级别值,我们将获得 1/2 重复项目。抱歉,我暂时没有办法解决这个问题。一旦我解决了,我会在这里回复。
    • 当然。谢谢。
    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2020-10-02
    • 2018-03-27
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多