Merge pull request #7466 from minrk/interact-method

allow interact(instancemethod)
This commit is contained in:
Thomas Kluyver 2015-01-15 10:22:27 -08:00
commit 85d84ceb58
2 changed files with 26 additions and 19 deletions

View File

@ -1,16 +1,7 @@
"""Interact with functions using widgets."""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
@ -30,10 +21,6 @@ from IPython.utils.traitlets import HasTraits, Any, Unicode
empty = Parameter.empty
#-----------------------------------------------------------------------------
# Classes and Functions
#-----------------------------------------------------------------------------
def _matches(o, pattern):
"""Match a pattern of types in a sequence."""
@ -251,7 +238,13 @@ def interact(__interact_f=None, **kwargs):
# ...
f = __interact_f
w = interactive(f, **kwargs)
f.widget = w
try:
f.widget = w
except AttributeError:
# some things (instancemethods) can't have attributes attached,
# so wrap in a lambda
f = lambda *args, **kwargs: __interact_f(*args, **kwargs)
f.widget = w
display(w)
return f
else:
@ -260,10 +253,7 @@ def interact(__interact_f=None, **kwargs):
# def f(*args, **kwargs):
# ...
def dec(f):
w = interactive(f, **kwargs)
f.widget = w
display(w)
return f
return interact(f, **kwargs)
return dec
def interact_manual(__interact_f=None, **kwargs):

View File

@ -365,6 +365,23 @@ def test_decorator_kwarg():
value=5,
)
@nt.with_setup(clear_display)
def test_interact_instancemethod():
class Foo(object):
def show(self, x):
print(x)
f = Foo()
with tt.monkeypatch(interaction, 'display', record_display):
g = interact(f.show, x=(1,10))
nt.assert_equal(len(displayed), 1)
w = displayed[0].children[0]
check_widget(w,
cls=widgets.IntSlider,
value=5,
)
@nt.with_setup(clear_display)
def test_decorator_no_call():
with tt.monkeypatch(interaction, 'display', record_display):