Widget 如何使用IPython小部件使一个参数的可能值依赖于另一个参数?

Widget 如何使用IPython小部件使一个参数的可能值依赖于另一个参数?,widget,ipython,jupyter,jupyter-notebook,ipywidgets,Widget,Ipython,Jupyter,Jupyter Notebook,Ipywidgets,假设我在Python中有一个简单的函数: def f(gender, name): if gender == 'male': return ranking_male(name) else: return ranking_female(name) 其中性别属于[“男性”、“女性”],而姓名属于[“亚当”、“约翰”、“马克斯”、“佛罗多”](如果性别是男性)或[“玛丽”、“莎拉”、“阿文”](否则) 我希望将interactivefromipywid

假设我在Python中有一个简单的函数:

def f(gender, name):
    if gender == 'male':
        return ranking_male(name)
    else:
        return ranking_female(name)
其中
性别
属于
[“男性”、“女性”]
,而
姓名
属于
[“亚当”、“约翰”、“马克斯”、“佛罗多”]
(如果
性别
男性
)或
[“玛丽”、“莎拉”、“阿文”]
(否则)

我希望将
interactive
from
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.

但我不知道如何做,也不知道如何解释doc字符串所说的内容。非常感谢您的帮助

我使用了嵌套的小部件来解决这个问题。它可以工作,但很难看,部分原因是它似乎不是
ipywidgets
()中的常见用例

给定函数
f(性别、姓名)
可以定义一个中间包装器:

def f_intermediate_wrapper(gender):
    if gender=="male":
        possible_names = ['Adam', 'John', 'Max', 'Frodo']
    else:
        possible_names = ['Mary', 'Sarah', 'Arwen']

    try:
        f_intermediate_wrapper.name_widget.widget.close()
    except AttributeError:
        pass

    f_intermediate_wrapper.name_widget = interact(f,
                                                  gender=widgets.fixed(gender),
                                                  name = possible_names)   
第一部分根据性别设置可能的名称选项

第二部分关闭先前评估中的
name\u小部件
(如果存在)。否则,每次更改性别时,它都会保留旧的名称列表,这些名称的性别是错误的()

第三部分为该性别创建一个可能的名字小部件,并将其存储在足够静态的地方。(否则,当您更改性别时,旧名称小部件将超出范围,您将无法关闭它。)

现在,您可以创建性别和姓名小部件:

gender_and_name_widget = interact(f_intermediate_wrapper,
                                  gender = ["male", "female"])
您还可以使用

gender_and_name_widget.name_widget.widget.result

例如,您有
品牌
车型
,而
车型
取决于
品牌

d = {'Volkswagen' : ['Tiguan', 'Passat', 'Polo', 'Touareg', 'Jetta'], 'Chevrolet' : ['TAHOE', 'CAMARO'] }

brand_widget = Dropdown( options=list(d.keys()),
                         value='Volkswagen',
                         description='Brand:',
                         style=style
                       )
model_widget = Dropdown( options=d['Volkswagen'],
                         value=None,
                         description='Model:',
                         style=style
                       )

def on_update_brand_widget(*args):
    model_widget.options = d[brand_widget.value]

brand_widget.observe(on_update_brand_widget, 'value')

非常有趣!谢谢你能解释一下这条线在干什么吗<代码>品牌小部件。观察(在品牌小部件上的“值”)回调注册: