forked from holoviz/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.py
518 lines (456 loc) · 19.4 KB
/
interact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
Interact with functions using widgets.
The interact Pane implemented in this module mirrors
ipywidgets.interact in its API and implementation. Large parts of the
code were copied directly from ipywidgets:
Copyright (c) Jupyter Development Team and PyViz Development Team.
Distributed under the terms of the Modified BSD License.
"""
from __future__ import absolute_import, division, unicode_literals
import types
from collections import OrderedDict
from inspect import getcallargs
from numbers import Real, Integral
from six import string_types
try: # Python >= 3.3
from inspect import signature, Parameter
from collections.abc import Iterable, Mapping
empty = Parameter.empty
except ImportError:
from collections import Iterable, Mapping
try:
from IPython.utils.signatures import signature, Parameter
empty = Parameter.empty
except:
signature, Parameter, empty = None, None, None
try:
from inspect import getfullargspec as check_argspec
except ImportError:
from inspect import getargspec as check_argspec # py2
import param
from .layout import Panel, Column, Row
from .pane import PaneBase, Pane, HTML
from .util import as_unicode
from .widgets import (Checkbox, TextInput, Widget, IntSlider, FloatSlider,
Select, DiscreteSlider, Button)
def _get_min_max_value(min, max, value=None, step=None):
"""Return min, max, value given input values with possible None."""
# Either min and max need to be given, or value needs to be given
if value is None:
if min is None or max is None:
raise ValueError('unable to infer range, value from: ({0}, {1}, {2})'.format(min, max, value))
diff = max - min
value = min + (diff / 2)
# Ensure that value has the same type as diff
if not isinstance(value, type(diff)):
value = min + (diff // 2)
else: # value is not None
if not isinstance(value, Real):
raise TypeError('expected a real number, got: %r' % value)
# Infer min/max from value
if value == 0:
# This gives (0, 1) of the correct type
vrange = (value, value + 1)
elif value > 0:
vrange = (-value, 3*value)
else:
vrange = (3*value, -value)
if min is None:
min = vrange[0]
if max is None:
max = vrange[1]
if step is not None:
# ensure value is on a step
tick = int((value - min) / step)
value = min + tick * step
if not min <= value <= max:
raise ValueError('value must be between min and max (min={0}, value={1}, max={2})'.format(min, value, max))
return min, max, value
def _yield_abbreviations_for_parameter(parameter, kwargs):
"""Get an abbreviation for a function parameter."""
name = parameter.name
kind = parameter.kind
ann = parameter.annotation
default = parameter.default
not_found = (name, empty, empty)
if kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY):
if name in kwargs:
value = kwargs.pop(name)
elif ann is not empty:
param.main.warning("Using function annotations to implicitly specify interactive controls is deprecated. Use an explicit keyword argument for the parameter instead.", DeprecationWarning)
value = ann
elif default is not empty:
value = default
if isinstance(value, (Iterable, Mapping)):
value = fixed(value)
else:
yield not_found
yield (name, value, default)
elif kind == Parameter.VAR_KEYWORD:
# In this case name=kwargs and we yield the items in kwargs with their keys.
for k, v in kwargs.copy().items():
kwargs.pop(k)
yield k, v, empty
def _matches(o, pattern):
"""Match a pattern of types in a sequence."""
if not len(o) == len(pattern):
return False
comps = zip(o,pattern)
return all(isinstance(obj,kind) for obj,kind in comps)
class interactive(PaneBase):
default_layout = param.ClassSelector(default=Column, class_=(Panel),
is_instance=False)
manual_update = param.Boolean(default=False, doc="""
Whether to update manually by clicking on button.""")
manual_name = param.String(default='Run Interact')
def __init__(self, object, params={}, **kwargs):
if signature is None:
raise ImportError('interact requires either recent Python version '
'(>=3.3 or IPython to inspect function signatures.')
super(interactive, self).__init__(object, **params)
new_kwargs = self.find_abbreviations(kwargs)
# Before we proceed, let's make sure that the user has passed a set of args+kwargs
# that will lead to a valid call of the function. This protects against unspecified
# and doubly-specified arguments.
try:
check_argspec(object)
except TypeError:
# if we can't inspect, we can't validate
pass
else:
getcallargs(object, **{n:v for n,v,_ in new_kwargs})
widgets = self.widgets_from_abbreviations(new_kwargs)
if self.manual_update:
widgets.append(('manual', Button(name=self.manual_name)))
self._widgets = OrderedDict(widgets)
self._pane = Pane(self.object(**self.kwargs), name=self.name)
self._inner_layout = Row(self._pane)
widgets = [widget for _, widget in widgets if isinstance(widget, Widget)]
if 'name' in params:
widgets.insert(0, HTML('<h2>%s</h2>' % self.name))
self.widget_box = Column(*widgets)
self.layout.objects = [self.widget_box, self._inner_layout]
self._link_widgets()
#----------------------------------------------------------------
# Model API
#----------------------------------------------------------------
def _get_model(self, doc, root=None, parent=None, comm=None):
return self._inner_layout._get_model(doc, root, parent, comm)
#----------------------------------------------------------------
# Callback API
#----------------------------------------------------------------
def _synced_params(self):
return []
def _link_widgets(self):
if self.manual_update:
widgets = [('manual', self._widgets['manual'])]
else:
widgets = self._widgets.items()
for name, widget in widgets:
def update_pane(change):
# Try updating existing pane
new_object = self.object(**self.kwargs)
pane_type = self.get_pane_type(new_object)
if type(self._pane) is pane_type:
if isinstance(new_object, (PaneBase, Panel)):
new_params = {k: v for k, v in new_object.get_param_values()
if k != 'name'}
self._pane.set_param(**new_params)
else:
self._pane.object = new_object
return
# Replace pane entirely
self._pane = Pane(new_object)
self._inner_layout[0] = self._pane
pname = 'clicks' if name == 'manual' else 'value'
watcher = widget.param.watch(update_pane, pname)
self._callbacks.append(watcher)
def _cleanup(self, root):
self._inner_layout._cleanup(root)
super(interactive, self)._cleanup(root)
#----------------------------------------------------------------
# Public API
#----------------------------------------------------------------
@property
def kwargs(self):
return {k: widget.value for k, widget in self._widgets.items()
if k != 'manual'}
def signature(self):
return signature(self.object)
def find_abbreviations(self, kwargs):
"""Find the abbreviations for the given function and kwargs.
Return (name, abbrev, default) tuples.
"""
new_kwargs = []
try:
sig = self.signature()
except (ValueError, TypeError):
# can't inspect, no info from function; only use kwargs
return [ (key, value, value) for key, value in kwargs.items() ]
for parameter in sig.parameters.values():
for name, value, default in _yield_abbreviations_for_parameter(parameter, kwargs):
if value is empty:
raise ValueError('cannot find widget or abbreviation for argument: {!r}'.format(name))
new_kwargs.append((name, value, default))
return new_kwargs
def widgets_from_abbreviations(self, seq):
"""Given a sequence of (name, abbrev, default) tuples, return a sequence of Widgets."""
result = []
for name, abbrev, default in seq:
if isinstance(abbrev, fixed):
widget = abbrev
else:
widget = self.widget_from_abbrev(abbrev, name, default)
if not (isinstance(widget, Widget) or isinstance(widget, fixed)):
if widget is None:
continue
else:
raise TypeError("{!r} is not a ValueWidget".format(widget))
result.append((name, widget))
return result
@classmethod
def applies(cls, object):
return isinstance(object, types.FunctionType)
@classmethod
def widget_from_abbrev(cls, abbrev, name, default=empty):
"""Build a ValueWidget instance given an abbreviation or Widget."""
if isinstance(abbrev, Widget):
return abbrev
if isinstance(abbrev, tuple):
widget = cls.widget_from_tuple(abbrev, name, default)
if default is not empty:
try:
widget.value = default
except Exception:
# ignore failure to set default
pass
return widget
# Try single value
widget = cls.widget_from_single_value(abbrev, name)
if widget is not None:
return widget
# Something iterable (list, dict, generator, ...). Note that str and
# tuple should be handled before, that is why we check this case last.
if isinstance(abbrev, Iterable):
widget = cls.widget_from_iterable(abbrev, name)
if default is not empty:
try:
widget.value = default
except Exception:
# ignore failure to set default
pass
return widget
# No idea...
return fixed(abbrev)
@staticmethod
def widget_from_single_value(o, name):
"""Make widgets from single values, which can be used as parameter defaults."""
if isinstance(o, string_types):
return TextInput(value=as_unicode(o), name=name)
elif isinstance(o, bool):
return Checkbox(value=o, name=name)
elif isinstance(o, Integral):
min, max, value = _get_min_max_value(None, None, o)
return IntSlider(value=o, start=min, end=max, name=name)
elif isinstance(o, Real):
min, max, value = _get_min_max_value(None, None, o)
return FloatSlider(value=o, start=min, end=max, name=name)
else:
return None
@staticmethod
def widget_from_tuple(o, name, default=empty):
"""Make widgets from a tuple abbreviation."""
int_default = (default is empty or isinstance(default, int))
if _matches(o, (Real, Real)):
min, max, value = _get_min_max_value(o[0], o[1])
if all(isinstance(_, Integral) for _ in o) and int_default:
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, name=name)
elif _matches(o, (Real, Real, Real)):
step = o[2]
if step <= 0:
raise ValueError("step must be >= 0, not %r" % step)
min, max, value = _get_min_max_value(o[0], o[1], step=step)
if all(isinstance(_, Integral) for _ in o) and int_default:
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, step=step, name=name)
elif _matches(o, (Real, Real, Real, Real)):
step = o[2]
if step <= 0:
raise ValueError("step must be >= 0, not %r" % step)
min, max, value = _get_min_max_value(o[0], o[1], value=o[3], step=step)
if all(isinstance(_, Integral) for _ in o):
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, step=step, name=name)
elif len(o) == 4:
min, max, value = _get_min_max_value(o[0], o[1], value=o[3])
if all(isinstance(_, Integral) for _ in [o[0], o[1], o[3]]):
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, name=name)
@staticmethod
def widget_from_iterable(o, name):
"""Make widgets from an iterable. This should not be done for
a string or tuple."""
# Select expects a dict or list, so we convert an arbitrary
# iterable to either of those.
values = list(o.values()) if isinstance(o, Mapping) else list(o)
widget_type = DiscreteSlider if all(param._is_number(v) for v in values) else Select
if isinstance(o, (list, dict)):
return widget_type(options=o, name=name)
elif isinstance(o, Mapping):
return widget_type(options=list(o.items()), name=name)
else:
return widget_type(options=list(o), name=name)
# Return a factory for interactive functions
@classmethod
def factory(cls):
options = dict(manual_update=False, manual_name="Run Interact")
return _InteractFactory(cls, options)
class _InteractFactory(object):
"""
Factory for instances of :class:`interactive`.
Arguments
---------
cls: class
The subclass of :class:`interactive` to construct.
options: dict
A dict of options used to construct the interactive
function. By default, this is returned by
``cls.default_options()``.
kwargs: dict
A dict of **kwargs to use for widgets.
"""
def __init__(self, cls, options, kwargs={}):
self.cls = cls
self.opts = options
self.kwargs = kwargs
def widget(self, f):
"""
Return an interactive function widget for the given function.
The widget is only constructed, not displayed nor attached to
the function.
Returns
-------
An instance of ``self.cls`` (typically :class:`interactive`).
Parameters
----------
f : function
The function to which the interactive widgets are tied.
"""
return self.cls(f, self.opts, **self.kwargs)
def __call__(self, __interact_f=None, **kwargs):
"""
Make the given function interactive by adding and displaying
the corresponding :class:`interactive` widget.
Expects the first argument to be a function. Parameters to this
function are widget abbreviations passed in as keyword arguments
(``**kwargs``). Can be used as a decorator (see examples).
Returns
-------
f : __interact_f with interactive widget attached to it.
Parameters
----------
__interact_f : function
The function to which the interactive widgets are tied. The `**kwargs`
should match the function signature. Passed to :func:`interactive()`
**kwargs : various, optional
An interactive widget is created for each keyword argument that is a
valid widget abbreviation. Passed to :func:`interactive()`
Examples
--------
Render an interactive text field that shows the greeting with the passed in
text::
# 1. Using interact as a function
def greeting(text="World"):
print("Hello {}".format(text))
interact(greeting, text="IPython Widgets")
# 2. Using interact as a decorator
@interact
def greeting(text="World"):
print("Hello {}".format(text))
# 3. Using interact as a decorator with named parameters
@interact(text="IPython Widgets")
def greeting(text="World"):
print("Hello {}".format(text))
Render an interactive slider widget and prints square of number::
# 1. Using interact as a function
def square(num=1):
print("{} squared is {}".format(num, num*num))
interact(square, num=5)
# 2. Using interact as a decorator
@interact
def square(num=2):
print("{} squared is {}".format(num, num*num))
# 3. Using interact as a decorator with named parameters
@interact(num=5)
def square(num=2):
print("{} squared is {}".format(num, num*num))
"""
# If kwargs are given, replace self by a new
# _InteractFactory with the updated kwargs
if kwargs:
params = list(interactive.param)
kw = dict(self.kwargs)
kw.update({k: v for k, v in kwargs.items() if k not in params})
opts = dict(self.opts, **{k: v for k, v in kwargs.items() if k in params})
self = type(self)(self.cls, opts, kw)
f = __interact_f
if f is None:
# This branch handles the case 3
# @interact(a=30, b=40)
# def f(*args, **kwargs):
# ...
#
# Simply return the new factory
return self
# positional arg support in: https://gist.github.com/8851331
# Handle the cases 1 and 2
# 1. interact(f, **kwargs)
# 2. @interact
# def f(*args, **kwargs):
# ...
w = self.widget(f)
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
return w.layout
def options(self, **kwds):
"""
Change options for interactive functions.
Returns
-------
A new :class:`_InteractFactory` which will apply the
options when called.
"""
opts = dict(self.opts)
for k in kwds:
if k not in opts:
raise ValueError("invalid option {!r}".format(k))
opts[k] = kwds[k]
return type(self)(self.cls, opts, self.kwargs)
interact = interactive.factory()
interact_manual = interact.options(manual_update=True, manual_name="Run Interact")
class fixed(param.Parameterized):
"""A pseudo-widget whose value is fixed and never synced to the client."""
value = param.Parameter(doc="Any Python object")
description = param.String(default='')
def __init__(self, value, **kwargs):
super(fixed, self).__init__(value=value, **kwargs)
def get_interact_value(self):
"""Return the value for this widget which should be passed to
interactive functions. Custom widgets can change this method
to process the raw value ``self.value``.
"""
return self.value