nehme ich diese einfache Funktion in Python haben:Wie ein Argument mögliche Werte abhängig von einem anderen Argument mit IPython Widgets haben?
def f(gender, name):
if gender == 'male':
return ranking_male(name)
else:
return ranking_female(name)
wo gender
zu ['male', 'female']
gehört während name
zu ['Adam', 'John', 'Max', 'Frodo']
gehört (wenn gender
ist male
) oder ['Mary', 'Sarah', 'Arwen']
(sonst).
Ich möchte interact
von ipywidgets
auf diese Funktion f
anwenden. Normalerweise würde man tun
from ipywidgets import interact
interact(f, gender = ('male', 'female'), name = ('Adam', 'John', 'Max', 'Frodo'))
Das Problem ist, dass die zulässigen Werte für name
hängen nun von dem Wert für gender
gewählt.
Ich habe versucht, es in der Dokumentation zu finden, konnte es aber nicht finden. Die einzige Sache, die ich denke, kann wichtig sein, ist Dies wird verwendet, um dynamische Benachrichtigungen von Merkmalsänderungen einzurichten.
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.
Aber ich habe keine Ahnung, wie es zu tun ist und nicht zu interpretieren, worüber der Doc-String spricht. Jede Hilfe wird sehr geschätzt!
sehr interessant! Vielen Dank! Kannst du erklären, was diese Linie macht? 'brand_widget.observe (on_update_brand_widget, 'value')' – gota
Callback-Registrierung: http://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Traitlet-events – mrgloom