|
|
|
|
@ -175,7 +175,7 @@ def interactive(__interact_f, **kwargs):
|
|
|
|
|
"""Build a group of widgets to interact with a function."""
|
|
|
|
|
f = __interact_f
|
|
|
|
|
co = kwargs.pop('clear_output', True)
|
|
|
|
|
choose = kwargs.pop('__choose', False)
|
|
|
|
|
manual = kwargs.pop('__manual', False)
|
|
|
|
|
kwargs_widgets = []
|
|
|
|
|
container = Box()
|
|
|
|
|
container.result = None
|
|
|
|
|
@ -197,9 +197,9 @@ def interactive(__interact_f, **kwargs):
|
|
|
|
|
c = [w for w in kwargs_widgets if isinstance(w, DOMWidget)]
|
|
|
|
|
|
|
|
|
|
# If we are only to run the function on demand, add a button to request this
|
|
|
|
|
if choose:
|
|
|
|
|
choose_button = Button(description="Run %s" % f.__name__)
|
|
|
|
|
c.append(choose_button)
|
|
|
|
|
if manual:
|
|
|
|
|
manual_button = Button(description="Run %s" % f.__name__)
|
|
|
|
|
c.append(manual_button)
|
|
|
|
|
container.children = c
|
|
|
|
|
|
|
|
|
|
# Build the callback
|
|
|
|
|
@ -210,8 +210,8 @@ def interactive(__interact_f, **kwargs):
|
|
|
|
|
container.kwargs[widget.description] = value
|
|
|
|
|
if co:
|
|
|
|
|
clear_output(wait=True)
|
|
|
|
|
if choose:
|
|
|
|
|
choose_button.disabled = True
|
|
|
|
|
if manual:
|
|
|
|
|
manual_button.disabled = True
|
|
|
|
|
try:
|
|
|
|
|
container.result = f(**container.kwargs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
@ -221,15 +221,15 @@ def interactive(__interact_f, **kwargs):
|
|
|
|
|
else:
|
|
|
|
|
ip.showtraceback()
|
|
|
|
|
finally:
|
|
|
|
|
if choose:
|
|
|
|
|
choose_button.disabled = False
|
|
|
|
|
if manual:
|
|
|
|
|
manual_button.disabled = False
|
|
|
|
|
|
|
|
|
|
# Wire up the widgets
|
|
|
|
|
# If we are doing choose running, the callback is only triggered by the button
|
|
|
|
|
# If we are doing manual running, the callback is only triggered by the button
|
|
|
|
|
# Otherwise, it is triggered for every trait change received
|
|
|
|
|
# On-demand running also suppresses running the fucntion with the initial parameters
|
|
|
|
|
if choose:
|
|
|
|
|
choose_button.on_click(call_f)
|
|
|
|
|
if manual:
|
|
|
|
|
manual_button.on_click(call_f)
|
|
|
|
|
else:
|
|
|
|
|
for widget in kwargs_widgets:
|
|
|
|
|
widget.on_trait_change(call_f, 'value')
|
|
|
|
|
@ -266,15 +266,15 @@ def interact(__interact_f=None, **kwargs):
|
|
|
|
|
return f
|
|
|
|
|
return dec
|
|
|
|
|
|
|
|
|
|
def interact_choose(__interact_f=None, **kwargs):
|
|
|
|
|
"""interact_choose(f, **kwargs)
|
|
|
|
|
def interact_manual(__interact_f=None, **kwargs):
|
|
|
|
|
"""interact_manual(f, **kwargs)
|
|
|
|
|
|
|
|
|
|
As `interact()`, generates widgets for each argument, but rather than running
|
|
|
|
|
the function after each widget change, adds a "Run" button and waits for it
|
|
|
|
|
to be clicked. Useful if the function is long-running and has several
|
|
|
|
|
parameters to change.
|
|
|
|
|
"""
|
|
|
|
|
return interact(__interact_f, __choose=True, **kwargs)
|
|
|
|
|
return interact(__interact_f, __manual=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
class fixed(HasTraits):
|
|
|
|
|
"""A pseudo-widget whose value is fixed and never synced to the client."""
|
|
|
|
|
|