如何解决多个依赖的小部件和功能
我正在寻找一种方法来集成以下问题中作为示例提供的代码,我可以在其中添加多个可以一次动态更新的函数:
Multiple dependent widgets (dropdown menu) on Jupyter notebook
以修改为例,我想在另一个函数中定义total
的时候动态更新,可能是和if __name__ == '__main__':
一起使用。我可以通过向 print_city 函数添加代码来添加许多计算和新小部件,但我会学习更好的方法,如果我们在代码中有 @interact,我无法理解这是如何工作的。
from ipywidgets import interact,Dropdown
geo = {'USA':['CHI','NYC'],'Russia':['MOW','LED']}
priceW = Dropdown(options = [105,100,80])
rateW = FloatText(200)
countryW = Dropdown(options = geo.keys())
cityW = Dropdown(options = geo[countryW.value])
districtW = Dropdown()
@interact(country=countryW,city=cityW,price=priceW,rate=rateW)
def print_city(country,city,price,rate):
cityW.options = geo[country]
priceW = price
rateW = rate
total = rate*price
print('Total: ',total)
如果有意义,我正在寻找这样的东西:
from ipywidgets import interact,80])
rateW = FloatText(200)
def main():
.....
countryW = Dropdown(options = geo.keys())
cityW = Dropdown(options = geo[countryW.value])
districtW = Dropdown()
@interact(country=countryW,rate):
cityW.options = geo[country]
priceW = price
rateW = rate
def totale():
total = rate*price
print('Total: ',total)
if __name__ == '__main__':
main()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。