如何使用IPython Widgets使参数可能值依赖于另一个参数?

时间:2016-06-23 15:01:51

标签: widget ipython jupyter jupyter-notebook ipywidgets

假设我在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'](如果gendermale)或['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.

但我不知道该怎么做,也不知道doc字符串在说什么。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

例如,您有brandmodel汽车,model取决于brand

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')

答案 1 :(得分:0)

我使用嵌套小部件来解决这个问题。它会起作用,但它很难看,部分是因为它似乎不是ipywidgetsthe following video)中的常见用例。

鉴于您的函数f(gender, name),您可以定义一个中间包装器:

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_widget(如果存在)。否则,每次更改性别时,它都会留下旧的名称列表,这是错误的性别(see discussion)。

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

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

gender_and_name_widget = interact(f_intermediate_wrapper,
                                  gender = ["male", "female"])

您可以使用

访问f(gender, name)的结果
gender_and_name_widget.name_widget.widget.result