using multiple depended widgets from Jupyter notebook and @interactive, Problem if Dropdown Menu has only one option

假装没事ソ 提交于 2020-06-17 15:49:31

问题


Hey so my code refers to this here : Multiple dependent widgets (dropdown menu) on Jupyter notebook

from ipywidgets import interact, Dropdown

    enfrom 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 @Fei Yao

(Sorry that i make an extra post but I didn't have enough 'points' to make a comment)

So my Problem is the following:

One of my Options in the dict e.g. 'NYC':['3'] has only one option. As a result the interactive selection there doesn't work and the print Function returns 2 lines in the end. I would like to 'select' even if there is only one option to choose, so that there is the final collected choice printed in one line.

I'm thankful for every help!

Greetings and sorry if it is a chaotic text, its the first time I'm posting sth here and I'm still learning to program. Have a nice Day!

来源:https://stackoverflow.com/questions/62258847/using-multiple-depended-widgets-from-jupyter-notebook-and-interactive-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!