2016-06-23 2 views
3

私はPythonで、この単純な機能を持っていると仮定:または['Mary', 'Sarah', 'Arwen'](その他)(gendermale場合)gendername一方['male', 'female']に属するIPythonウィジェットで別の引数に依存する引数を取る方法はありますか?

def f(gender, name): 
    if gender == 'male': 
     return ranking_male(name) 
    else: 
     return ranking_female(name) 

['Adam', 'John', 'Max', 'Frodo']に属します。

interactipywidgetsからfに適用したいと考えています。通常1は、問題がnameための許容値は、今genderのために選択した値に依存していることである

from ipywidgets import interact 
interact(f, gender = ('male', 'female'), name = ('Adam', 'John', 'Max', 'Frodo')) 

を行うだろう。

ドキュメントで見つけようとしましたが見つかりませんでした。私が重要と考える唯一のことは、 です。これは、特性変化の動的通知を設定するために使用されます。

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. 

しかし、私はそれをどうやってやっているのか、また、ドキュメント文字列が何を話しているのかを理解することはできません。どんな助けでも大歓迎です!

答えて

1

を使用してf(gender, name)の結果にアクセスすることができます。

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

非常に興味深い!ありがとう!この行が何をしているのか説明できますか? 'brand_widget.observe(on_update_brand_widget、 'value')' – gota

+0

コールバック登録:http://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Traitlet-events – mrgloom

0

私はこの問題を解決するためにネストされたウィジェットを使用しました。それはうまくいくでしょうが、これは部分的にはipywidgetssee discussion)の一般的な使用例ではないように見えるため、醜いです。あなたの関数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が存在する場合は、2番目の部分がname_widgetを閉じます。それ以外の場合は、性別を変更するたびに、間違った性別(see example)の古い名前リストが残されます。

3番目の部分は、その性別の可能な名前の名前ウィジェットを作成し、十分に静的な場所に格納します。 (そうでなければ、あなたが性別を変更すると、古い名前のウィジェットがスコープ外になり、あなたはそれを閉じることはできません。)

今、あなたはあなたの性別と名前のウィジェットを作成することができます

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

そして、あなたはあなたが車のbrandmodelを持っているとmodelbrandに依存例えば

gender_and_name_widget.name_widget.widget.result 
関連する問題