【发布时间】:2016-10-26 00:10:47
【问题描述】:
假设我在 Python 中有这个简单的函数:
def f(gender, name):
if gender == 'male':
return ranking_male(name)
else:
return ranking_female(name)
其中gender 属于['male', 'female'] 而name 属于['Adam', 'John', 'Max', 'Frodo'](如果gender 是male)或['Mary', 'Sarah', 'Arwen'](否则)。
我希望将interact 从ipywidgets 应用到这个函数f。通常一个人会这样做
from ipywidgets import interact
interact(f, gender = ('male', 'female'), name = ('Adam', 'John', 'Max', 'Frodo'))
问题是name 的允许值现在取决于为gender 选择的值。
我试图在文档中找到它,但找不到。我认为唯一可能重要的是 这用于设置特征更改的动态通知。
Parameters
----------
handler : callable
A callable that is called when a trait changes. Its
signature should be ``handler(change)``, where ``change```is a
dictionary. The change dictionary at least holds a 'type' key.
* ``type``: the type of notification.
Other keys may be passed depending on the value of 'type'. In the
case where type is 'change', we also have the following keys:
* ``owner`` : the HasTraits instance
* ``old`` : the old value of the modified trait attribute
* ``new`` : the new value of the modified trait attribute
* ``name`` : the name of the modified trait attribute.
names : list, str, All
If names is All, the handler will apply to all traits. If a list
of str, handler will apply to all names in the list. If a
str, the handler will apply just to that name.
type : str, All (default: 'change')
The type of notification to filter by. If equal to All, then all
notifications are passed to the observe handler.
但我不知道该怎么做,也不知道文档字符串在说什么。非常感谢任何帮助!
【问题讨论】:
标签: widget ipython jupyter jupyter-notebook ipywidgets