问题
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