hvplot 0.9.3a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hvplot/__init__.py +322 -0
- hvplot/_version.py +16 -0
- hvplot/backend_transforms.py +329 -0
- hvplot/converter.py +2855 -0
- hvplot/cudf.py +26 -0
- hvplot/dask.py +42 -0
- hvplot/data/crime.csv +56 -0
- hvplot/datasets.yaml +48 -0
- hvplot/fugue.py +64 -0
- hvplot/ibis.py +21 -0
- hvplot/intake.py +32 -0
- hvplot/interactive.py +968 -0
- hvplot/networkx.py +625 -0
- hvplot/pandas.py +30 -0
- hvplot/plotting/__init__.py +63 -0
- hvplot/plotting/andrews_curves.py +99 -0
- hvplot/plotting/core.py +2288 -0
- hvplot/plotting/lag_plot.py +34 -0
- hvplot/plotting/parallel_coordinates.py +85 -0
- hvplot/plotting/scatter_matrix.py +220 -0
- hvplot/polars.py +21 -0
- hvplot/sample_data.py +30 -0
- hvplot/streamz.py +21 -0
- hvplot/tests/__init__.py +0 -0
- hvplot/tests/conftest.py +44 -0
- hvplot/tests/data/README.md +5 -0
- hvplot/tests/data/RGB-red.byte.tif +0 -0
- hvplot/tests/plotting/__init__.py +0 -0
- hvplot/tests/plotting/testcore.py +108 -0
- hvplot/tests/plotting/testohlc.py +34 -0
- hvplot/tests/plotting/testscattermatrix.py +138 -0
- hvplot/tests/test_links.py +99 -0
- hvplot/tests/testbackend_transforms.py +89 -0
- hvplot/tests/testcharts.py +452 -0
- hvplot/tests/testfugue.py +46 -0
- hvplot/tests/testgeo.py +468 -0
- hvplot/tests/testgeowithoutgv.py +60 -0
- hvplot/tests/testgridplots.py +259 -0
- hvplot/tests/testhelp.py +75 -0
- hvplot/tests/testibis.py +17 -0
- hvplot/tests/testinteractive.py +1442 -0
- hvplot/tests/testnetworkx.py +26 -0
- hvplot/tests/testoperations.py +385 -0
- hvplot/tests/testoptions.py +596 -0
- hvplot/tests/testoverrides.py +74 -0
- hvplot/tests/testpanel.py +70 -0
- hvplot/tests/testpatch.py +135 -0
- hvplot/tests/testplotting.py +69 -0
- hvplot/tests/teststreaming.py +28 -0
- hvplot/tests/testtransforms.py +39 -0
- hvplot/tests/testui.py +383 -0
- hvplot/tests/testutil.py +378 -0
- hvplot/tests/util.py +82 -0
- hvplot/ui.py +1032 -0
- hvplot/util.py +677 -0
- hvplot/utilities.py +129 -0
- hvplot/xarray.py +62 -0
- hvplot-0.9.3a1.dist-info/LICENSE +29 -0
- hvplot-0.9.3a1.dist-info/METADATA +243 -0
- hvplot-0.9.3a1.dist-info/RECORD +63 -0
- hvplot-0.9.3a1.dist-info/WHEEL +5 -0
- hvplot-0.9.3a1.dist-info/entry_points.txt +2 -0
- hvplot-0.9.3a1.dist-info/top_level.txt +1 -0
hvplot/interactive.py
ADDED
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
"""
|
|
2
|
+
interactive API
|
|
3
|
+
|
|
4
|
+
How Interactive works
|
|
5
|
+
---------------------
|
|
6
|
+
|
|
7
|
+
`Interactive` is a wrapper around a Python object that lets users create
|
|
8
|
+
interactive pipelines by calling existing APIs on an object with dynamic
|
|
9
|
+
parameters or widgets.
|
|
10
|
+
|
|
11
|
+
An `Interactive` instance watches what operations are applied to the object.
|
|
12
|
+
|
|
13
|
+
To do so, each operation returns a new `Interactive` instance - the creation
|
|
14
|
+
of a new instance being taken care of by the `_clone` method - which allows
|
|
15
|
+
the next operation to be recorded, and so on and so forth. E.g. `dfi.head()`
|
|
16
|
+
first records that the `'head'` attribute is accessed, this is achieved
|
|
17
|
+
by overriding `__getattribute__`. A new interactive object is returned,
|
|
18
|
+
which will then record that it is being called, and that new object will be
|
|
19
|
+
itself called as `Interactive` implements `__call__`. `__call__` returns
|
|
20
|
+
another `Interactive` instance.
|
|
21
|
+
|
|
22
|
+
Note that under the hood even more `Interactive` instances may be created,
|
|
23
|
+
but this is the gist of it.
|
|
24
|
+
|
|
25
|
+
To be able to watch all the potential operations that may be applied to an
|
|
26
|
+
object, `Interactive` implements on top of `__getattribute__` and
|
|
27
|
+
`__call__`:
|
|
28
|
+
|
|
29
|
+
- operators such as `__gt__`, `__add__`, etc.
|
|
30
|
+
- the builtin functions `__abs__` and `__round__`
|
|
31
|
+
- `__getitem__`
|
|
32
|
+
- `__array_ufunc__`
|
|
33
|
+
|
|
34
|
+
The `_depth` attribute starts at 0 and is incremented by 1 every time
|
|
35
|
+
a new `Interactive` instance is created part of a chain.
|
|
36
|
+
The root instance in an expression has a `_depth` of 0. An expression can
|
|
37
|
+
consist of multiple chains, such as `dfi[dfi.A > 1]`, as the `Interactive`
|
|
38
|
+
instance is referenced twice in the expression. As a consequence `_depth`
|
|
39
|
+
is not the total count of `Interactive` instance creations of a pipeline,
|
|
40
|
+
it is the count of instances created in the outer chain. In the example, that
|
|
41
|
+
would be `dfi[]`. `Interactive` instances don't have references about
|
|
42
|
+
the instances that created them or that they create, they just know their
|
|
43
|
+
current location in a chain thanks to `_depth`. However, as some parameters
|
|
44
|
+
need to be passed down the whole pipeline, they do have to propagate. E.g.
|
|
45
|
+
in `dfi.interactive(width=200)`, `width=200` will be propagated as `kwargs`.
|
|
46
|
+
|
|
47
|
+
Recording the operations applied to an object in a pipeline is done
|
|
48
|
+
by gradually building a so-called "dim expression", or "dim transform",
|
|
49
|
+
which is an expression language provided by HoloViews. dim transform
|
|
50
|
+
objects are a way to express transforms on `Dataset`s, a `Dataset` being
|
|
51
|
+
another HoloViews object that is a wrapper around common data structures
|
|
52
|
+
such as Pandas/Dask/... Dataframes/Series, Xarray Dataset/DataArray, etc.
|
|
53
|
+
For instance a Python expression such as `(series + 2).head()` can be
|
|
54
|
+
expressed with a dim transform whose repr will be `(dim('*').pd+2).head(2)`,
|
|
55
|
+
effectively showing that the dim transform has recorded the different
|
|
56
|
+
operations that are meant to be applied to the data.
|
|
57
|
+
The `_transform` attribute stores the dim transform.
|
|
58
|
+
|
|
59
|
+
The `_obj` attribute holds the original data structure that feeds the
|
|
60
|
+
pipeline. All the `Interactive` instances created while parsing the
|
|
61
|
+
pipeline share the same `_obj` object. And they all wrap it in a `Dataset`
|
|
62
|
+
instance, and all apply the current dim transform they are aware of to
|
|
63
|
+
the original data structure to compute the intermediate state of the data,
|
|
64
|
+
that is stored it in the `_current_` attribute. Doing so is particularly
|
|
65
|
+
useful in Notebook sessions, as this allows to inspect the transformed
|
|
66
|
+
object at any point of the pipeline, and as such provide correct
|
|
67
|
+
auto-completion and docstrings. E.g. executing `dfi.A.max?` in a Notebook
|
|
68
|
+
will correctly return the docstring of the Pandas Series `.max()` method,
|
|
69
|
+
as the pipeline evaluates `dfi.A` to hold a current object `_current` that
|
|
70
|
+
is a Pandas Series, and no longer and DataFrame.
|
|
71
|
+
|
|
72
|
+
The `_obj` attribute is implemented as a property which gets/sets the value
|
|
73
|
+
from a list that contains the shared attribute. This is required for the
|
|
74
|
+
"function as input" to be able to update the object from a callback set up
|
|
75
|
+
on the root Interactive instance.
|
|
76
|
+
|
|
77
|
+
Internally interactive holds the current evaluated state on the `_current_`
|
|
78
|
+
attribute, when some parameter in the interactive pipeline is changed
|
|
79
|
+
the pipeline is marked as `_dirty`. This means that the next time `_current`
|
|
80
|
+
is accessed the pipeline will be re-evaluated to get the up-to-date
|
|
81
|
+
current value.
|
|
82
|
+
|
|
83
|
+
The `_method` attribute is a string that temporarily stores the method/attr
|
|
84
|
+
accessed on the object, e.g. `_method` is 'head' in `dfi.head()`, until the
|
|
85
|
+
Interactive instance created in the pipeline is called at which point `_method`
|
|
86
|
+
is reset to None. In cases such as `dfi.head` or `dfi.A`, `_method` is not
|
|
87
|
+
(yet) reset to None. At this stage the Interactive instance returned has
|
|
88
|
+
its `_current` attribute not updated, e.g. `dfi.A._current` is still the
|
|
89
|
+
original dataframe, not the 'A' series. Keeping `_method` is thus useful for
|
|
90
|
+
instance to display `dfi.A`, as the evaluation of the object will check
|
|
91
|
+
whether `_method` is set or not, and if it's set it will use it to compute
|
|
92
|
+
the object returned, e.g. the series `df.A` or the method `df.head`, and
|
|
93
|
+
display its repr.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
import abc
|
|
97
|
+
import operator
|
|
98
|
+
import sys
|
|
99
|
+
|
|
100
|
+
from functools import partial
|
|
101
|
+
from packaging.version import Version
|
|
102
|
+
from types import FunctionType, MethodType
|
|
103
|
+
|
|
104
|
+
import holoviews as hv
|
|
105
|
+
import pandas as pd
|
|
106
|
+
import panel as pn
|
|
107
|
+
import param
|
|
108
|
+
|
|
109
|
+
from panel.layout import Column, Row, VSpacer, HSpacer
|
|
110
|
+
from panel.util import get_method_owner, full_groupby
|
|
111
|
+
from panel.widgets.base import Widget
|
|
112
|
+
|
|
113
|
+
from .converter import HoloViewsConverter
|
|
114
|
+
from .util import (
|
|
115
|
+
_flatten,
|
|
116
|
+
bokeh3,
|
|
117
|
+
is_tabular,
|
|
118
|
+
is_xarray,
|
|
119
|
+
is_xarray_dataarray,
|
|
120
|
+
_convert_col_names_to_str,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _find_widgets(op):
|
|
125
|
+
widgets = []
|
|
126
|
+
op_args = list(op['args']) + list(op['kwargs'].values())
|
|
127
|
+
op_args = _flatten(op_args)
|
|
128
|
+
for op_arg in op_args:
|
|
129
|
+
# Find widgets introduced as `widget` in an expression
|
|
130
|
+
if isinstance(op_arg, Widget) and op_arg not in widgets:
|
|
131
|
+
widgets.append(op_arg)
|
|
132
|
+
# TODO: Find how to execute this path?
|
|
133
|
+
if isinstance(op_arg, hv.dim):
|
|
134
|
+
for nested_op in op_arg.ops:
|
|
135
|
+
for widget in _find_widgets(nested_op):
|
|
136
|
+
if widget not in widgets:
|
|
137
|
+
widgets.append(widget)
|
|
138
|
+
# Find Ipywidgets
|
|
139
|
+
if 'ipywidgets' in sys.modules:
|
|
140
|
+
from ipywidgets import Widget as IPyWidget
|
|
141
|
+
|
|
142
|
+
if isinstance(op_arg, IPyWidget) and op_arg not in widgets:
|
|
143
|
+
widgets.append(op_arg)
|
|
144
|
+
# Find widgets introduced as `widget.param.value` in an expression
|
|
145
|
+
if (
|
|
146
|
+
isinstance(op_arg, param.Parameter)
|
|
147
|
+
and isinstance(op_arg.owner, pn.widgets.Widget)
|
|
148
|
+
and op_arg.owner not in widgets
|
|
149
|
+
):
|
|
150
|
+
widgets.append(op_arg.owner)
|
|
151
|
+
if isinstance(op_arg, slice):
|
|
152
|
+
if Version(hv.__version__) < Version('1.15.1'):
|
|
153
|
+
raise ValueError(
|
|
154
|
+
'Using interactive with slices needs to have '
|
|
155
|
+
'Holoviews 1.15.1 or greater installed.'
|
|
156
|
+
)
|
|
157
|
+
nested_op = {'args': [op_arg.start, op_arg.stop, op_arg.step], 'kwargs': {}}
|
|
158
|
+
for widget in _find_widgets(nested_op):
|
|
159
|
+
if widget not in widgets:
|
|
160
|
+
widgets.append(widget)
|
|
161
|
+
return widgets
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class Interactive:
|
|
165
|
+
"""
|
|
166
|
+
The `.interactive` API enhances the API of data analysis libraries
|
|
167
|
+
like Pandas, Dask, and Xarray, by allowing to replace in a pipeline
|
|
168
|
+
static values by dynamic widgets. When displayed, an interactive
|
|
169
|
+
pipeline will incorporate the dynamic widgets that control it, as long
|
|
170
|
+
as its normal output that will automatically be updated as soon as a
|
|
171
|
+
widget value is changed.
|
|
172
|
+
|
|
173
|
+
`Interactive` can be instantiated with an object. However the recommended
|
|
174
|
+
approach is to instantiate it via the `.interactive` accessor that is
|
|
175
|
+
available on a data structure when it has been patched, e.g. after
|
|
176
|
+
executing `import hvplot.pandas`. The accessor can also be called which
|
|
177
|
+
allows to pass down kwargs.
|
|
178
|
+
|
|
179
|
+
A pipeline can then be created from this object, the pipeline will render
|
|
180
|
+
with its widgets and its interactive output.
|
|
181
|
+
|
|
182
|
+
Reference: https://hvplot.holoviz.org/user_guide/Interactive.html
|
|
183
|
+
|
|
184
|
+
Parameters
|
|
185
|
+
----------
|
|
186
|
+
obj: DataFrame, Series, DataArray, DataSet
|
|
187
|
+
A supported data structure object
|
|
188
|
+
loc : str, optional
|
|
189
|
+
Widget(s) location, one of 'bottom_left', 'bottom_right', 'right'
|
|
190
|
+
'top-right', 'top-left' and 'left'. By default 'top_left'
|
|
191
|
+
center : bool, optional
|
|
192
|
+
Whether to center to pipeline output, by default False
|
|
193
|
+
max_rows : int, optional
|
|
194
|
+
Maximum number of rows displayed, only used when the output is a
|
|
195
|
+
dataframe, by default 100
|
|
196
|
+
kwargs: optional
|
|
197
|
+
Optional kwargs that are passed down to customize the displayed
|
|
198
|
+
object. E.g. if the output is a DataFrame `width=200` will set
|
|
199
|
+
the size of the DataFrame Pane that renders it.
|
|
200
|
+
|
|
201
|
+
Examples
|
|
202
|
+
--------
|
|
203
|
+
Instantiate it from an object:
|
|
204
|
+
>>> dfi = Interactive(df)
|
|
205
|
+
|
|
206
|
+
Or with the `.interactive` accessor when the object is patched:
|
|
207
|
+
>>> import hvplot.pandas
|
|
208
|
+
>>> dfi = df.interactive
|
|
209
|
+
>>> dfi = df.interactive(width=200)
|
|
210
|
+
|
|
211
|
+
Create interactive pipelines from the `Interactive` object:
|
|
212
|
+
>>> widget = panel.widgets.IntSlider(value=1, start=1, end=5)
|
|
213
|
+
>>> dfi.head(widget)
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
# TODO: Why?
|
|
217
|
+
__metaclass__ = abc.ABCMeta
|
|
218
|
+
|
|
219
|
+
# Hackery to support calls to the classic `.plot` API, see `_get_ax_fn`
|
|
220
|
+
# for more hacks!
|
|
221
|
+
_fig = None
|
|
222
|
+
|
|
223
|
+
def __new__(cls, obj, **kwargs):
|
|
224
|
+
# __new__ implemented to support functions as input, e.g.
|
|
225
|
+
# hvplot.find(foo, widget).interactive().max()
|
|
226
|
+
if 'fn' in kwargs:
|
|
227
|
+
fn = kwargs.pop('fn')
|
|
228
|
+
elif isinstance(obj, (FunctionType, MethodType)):
|
|
229
|
+
fn = pn.panel(obj, lazy=True)
|
|
230
|
+
obj = fn.eval(obj)
|
|
231
|
+
else:
|
|
232
|
+
fn = None
|
|
233
|
+
clss = cls
|
|
234
|
+
for subcls in cls.__subclasses__():
|
|
235
|
+
if subcls.applies(obj):
|
|
236
|
+
clss = subcls
|
|
237
|
+
inst = super().__new__(clss)
|
|
238
|
+
inst._shared_obj = kwargs.get('_shared_obj', [obj])
|
|
239
|
+
inst._fn = fn
|
|
240
|
+
return inst
|
|
241
|
+
|
|
242
|
+
@classmethod
|
|
243
|
+
def applies(cls, obj):
|
|
244
|
+
"""
|
|
245
|
+
Subclasses must implement applies and return a boolean to indicate
|
|
246
|
+
whether the subclass should apply or not to the obj.
|
|
247
|
+
"""
|
|
248
|
+
return True
|
|
249
|
+
|
|
250
|
+
def __init__(
|
|
251
|
+
self,
|
|
252
|
+
obj,
|
|
253
|
+
transform=None,
|
|
254
|
+
fn=None,
|
|
255
|
+
plot=False,
|
|
256
|
+
depth=0,
|
|
257
|
+
loc='top_left',
|
|
258
|
+
center=False,
|
|
259
|
+
dmap=False,
|
|
260
|
+
inherit_kwargs={},
|
|
261
|
+
max_rows=100,
|
|
262
|
+
method=None,
|
|
263
|
+
_shared_obj=None,
|
|
264
|
+
_current=None,
|
|
265
|
+
**kwargs,
|
|
266
|
+
):
|
|
267
|
+
# _init is used to prevent to __getattribute__ to execute its
|
|
268
|
+
# specialized code.
|
|
269
|
+
self._init = False
|
|
270
|
+
self._method = method
|
|
271
|
+
if transform is None:
|
|
272
|
+
dim = '*'
|
|
273
|
+
transform = hv.util.transform.dim
|
|
274
|
+
if is_xarray(obj):
|
|
275
|
+
transform = hv.util.transform.xr_dim
|
|
276
|
+
if is_xarray_dataarray(obj):
|
|
277
|
+
dim = obj.name
|
|
278
|
+
if dim is None:
|
|
279
|
+
raise ValueError(
|
|
280
|
+
'Cannot use interactive API on DataArray without name.'
|
|
281
|
+
'Assign a name to the DataArray and try again.'
|
|
282
|
+
)
|
|
283
|
+
elif is_tabular(obj):
|
|
284
|
+
transform = hv.util.transform.df_dim
|
|
285
|
+
self._transform = transform(dim)
|
|
286
|
+
else:
|
|
287
|
+
self._transform = transform
|
|
288
|
+
self._plot = plot
|
|
289
|
+
self._depth = depth
|
|
290
|
+
self._loc = loc
|
|
291
|
+
self._center = center
|
|
292
|
+
self._dmap = dmap
|
|
293
|
+
# TODO: What's the real use of inherit_kwargs? So far I've only seen
|
|
294
|
+
# it containing 'ax'
|
|
295
|
+
self._inherit_kwargs = inherit_kwargs
|
|
296
|
+
self._max_rows = max_rows
|
|
297
|
+
self._kwargs = kwargs
|
|
298
|
+
ds = hv.Dataset(_convert_col_names_to_str(self._obj))
|
|
299
|
+
if _current is not None:
|
|
300
|
+
self._current_ = _current
|
|
301
|
+
else:
|
|
302
|
+
self._current_ = self._transform.apply(ds, keep_index=True, compute=False)
|
|
303
|
+
self._init = True
|
|
304
|
+
self._dirty = False
|
|
305
|
+
self.hvplot = _hvplot(self)
|
|
306
|
+
self._setup_invalidations(depth)
|
|
307
|
+
|
|
308
|
+
@property
|
|
309
|
+
def _obj(self):
|
|
310
|
+
return self._shared_obj[0]
|
|
311
|
+
|
|
312
|
+
@_obj.setter
|
|
313
|
+
def _obj(self, obj):
|
|
314
|
+
if self._shared_obj is None:
|
|
315
|
+
self._shared_obj = [obj]
|
|
316
|
+
else:
|
|
317
|
+
self._shared_obj[0] = obj
|
|
318
|
+
|
|
319
|
+
@property
|
|
320
|
+
def _current(self):
|
|
321
|
+
if self._dirty:
|
|
322
|
+
self.eval()
|
|
323
|
+
return self._current_
|
|
324
|
+
|
|
325
|
+
@property
|
|
326
|
+
def _fn_params(self):
|
|
327
|
+
if self._fn is None:
|
|
328
|
+
deps = []
|
|
329
|
+
elif isinstance(self._fn, pn.param.ParamFunction):
|
|
330
|
+
dinfo = getattr(self._fn.object, '_dinfo', {})
|
|
331
|
+
deps = list(dinfo.get('dependencies', [])) + list(dinfo.get('kw', {}).values())
|
|
332
|
+
else:
|
|
333
|
+
# TODO: Find how to execute that path?
|
|
334
|
+
parameterized = get_method_owner(self._fn.object)
|
|
335
|
+
deps = parameterized.param.method_dependencies(self._fn.object.__name__)
|
|
336
|
+
return deps
|
|
337
|
+
|
|
338
|
+
@property
|
|
339
|
+
def _params(self):
|
|
340
|
+
ps = self._fn_params
|
|
341
|
+
for k, p in self._transform.params.items():
|
|
342
|
+
if k == 'ax' or p in ps:
|
|
343
|
+
continue
|
|
344
|
+
ps.append(p)
|
|
345
|
+
return ps
|
|
346
|
+
|
|
347
|
+
def _setup_invalidations(self, depth=0):
|
|
348
|
+
"""
|
|
349
|
+
Since the parameters of the pipeline can change at any time
|
|
350
|
+
we have to invalidate the internal state of the pipeline.
|
|
351
|
+
To handle both invalidations of the inputs of the pipeline
|
|
352
|
+
and the pipeline itself we set up watchers on both.
|
|
353
|
+
|
|
354
|
+
1. The first invalidation we have to set up is to re-evaluate
|
|
355
|
+
the function that feeds the pipeline. Only the root node of
|
|
356
|
+
a pipeline has to perform this invalidation because all
|
|
357
|
+
leaf nodes inherit the same shared_obj. This avoids
|
|
358
|
+
evaluating the same function for every branch of the pipeline.
|
|
359
|
+
2. The second invalidation is for the pipeline itself, i.e.
|
|
360
|
+
if any parameter changes we have to notify the pipeline that
|
|
361
|
+
it has to re-evaluate the pipeline. This is done by marking
|
|
362
|
+
the pipeline as `_dirty`. The next time the `_current` value
|
|
363
|
+
is requested we then run and `.eval()` pass that re-executes
|
|
364
|
+
the pipeline.
|
|
365
|
+
"""
|
|
366
|
+
if self._fn is not None and depth == 0:
|
|
367
|
+
for _, params in full_groupby(self._fn_params, lambda x: id(x.owner)):
|
|
368
|
+
params[0].owner.param.watch(self._update_obj, [p.name for p in params])
|
|
369
|
+
for _, params in full_groupby(self._params, lambda x: id(x.owner)):
|
|
370
|
+
params[0].owner.param.watch(self._invalidate_current, [p.name for p in params])
|
|
371
|
+
|
|
372
|
+
def _invalidate_current(self, *events):
|
|
373
|
+
self._dirty = True
|
|
374
|
+
|
|
375
|
+
def _update_obj(self, *args):
|
|
376
|
+
self._obj = self._fn.eval(self._fn.object)
|
|
377
|
+
|
|
378
|
+
@property
|
|
379
|
+
def _callback(self):
|
|
380
|
+
def evaluate_inner():
|
|
381
|
+
obj = self.eval()
|
|
382
|
+
if isinstance(obj, pd.DataFrame):
|
|
383
|
+
return pn.pane.DataFrame(obj, max_rows=self._max_rows, **self._kwargs)
|
|
384
|
+
return obj
|
|
385
|
+
|
|
386
|
+
params = self._params
|
|
387
|
+
if params:
|
|
388
|
+
|
|
389
|
+
@pn.depends(*params)
|
|
390
|
+
def evaluate(*args, **kwargs):
|
|
391
|
+
return evaluate_inner()
|
|
392
|
+
else:
|
|
393
|
+
|
|
394
|
+
def evaluate():
|
|
395
|
+
return evaluate_inner()
|
|
396
|
+
|
|
397
|
+
return evaluate
|
|
398
|
+
|
|
399
|
+
def _clone(
|
|
400
|
+
self,
|
|
401
|
+
transform=None,
|
|
402
|
+
plot=None,
|
|
403
|
+
loc=None,
|
|
404
|
+
center=None,
|
|
405
|
+
dmap=None,
|
|
406
|
+
copy=False,
|
|
407
|
+
max_rows=None,
|
|
408
|
+
**kwargs,
|
|
409
|
+
):
|
|
410
|
+
plot = self._plot or plot
|
|
411
|
+
transform = transform or self._transform
|
|
412
|
+
loc = self._loc if loc is None else loc
|
|
413
|
+
center = self._center if center is None else center
|
|
414
|
+
dmap = self._dmap if dmap is None else dmap
|
|
415
|
+
max_rows = self._max_rows if max_rows is None else max_rows
|
|
416
|
+
depth = self._depth + 1
|
|
417
|
+
if copy:
|
|
418
|
+
kwargs = dict(
|
|
419
|
+
self._kwargs,
|
|
420
|
+
_current=self._current,
|
|
421
|
+
inherit_kwargs=self._inherit_kwargs,
|
|
422
|
+
method=self._method,
|
|
423
|
+
**kwargs,
|
|
424
|
+
)
|
|
425
|
+
else:
|
|
426
|
+
kwargs = dict(self._inherit_kwargs, **dict(self._kwargs, **kwargs))
|
|
427
|
+
return type(self)(
|
|
428
|
+
self._obj,
|
|
429
|
+
fn=self._fn,
|
|
430
|
+
transform=transform,
|
|
431
|
+
plot=plot,
|
|
432
|
+
depth=depth,
|
|
433
|
+
loc=loc,
|
|
434
|
+
center=center,
|
|
435
|
+
dmap=dmap,
|
|
436
|
+
_shared_obj=self._shared_obj,
|
|
437
|
+
max_rows=max_rows,
|
|
438
|
+
**kwargs,
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
def _repr_mimebundle_(self, include=[], exclude=[]):
|
|
442
|
+
return self.layout()._repr_mimebundle_()
|
|
443
|
+
|
|
444
|
+
def __dir__(self):
|
|
445
|
+
current = self._current
|
|
446
|
+
if self._method:
|
|
447
|
+
current = getattr(current, self._method)
|
|
448
|
+
extras = {attr for attr in dir(current) if not attr.startswith('_')}
|
|
449
|
+
if is_tabular(current) and hasattr(current, 'columns'):
|
|
450
|
+
extras |= set(current.columns)
|
|
451
|
+
try:
|
|
452
|
+
return sorted(set(super().__dir__()) | extras)
|
|
453
|
+
except Exception:
|
|
454
|
+
return sorted(set(dir(type(self))) | set(self.__dict__) | extras)
|
|
455
|
+
|
|
456
|
+
def _resolve_accessor(self):
|
|
457
|
+
if not self._method:
|
|
458
|
+
# No method is yet set, as in `dfi.A`, so return a copied clone.
|
|
459
|
+
return self._clone(copy=True)
|
|
460
|
+
# This is executed when one runs e.g. `dfi.A > 1`, in which case after
|
|
461
|
+
# dfi.A the _method 'A' is set (in __getattribute__) which allows
|
|
462
|
+
# _resolve_accessor to keep building the transform dim expression.
|
|
463
|
+
transform = type(self._transform)(self._transform, self._method, accessor=True)
|
|
464
|
+
transform._ns = self._current
|
|
465
|
+
inherit_kwargs = {}
|
|
466
|
+
if self._method == 'plot':
|
|
467
|
+
inherit_kwargs['ax'] = self._get_ax_fn()
|
|
468
|
+
try:
|
|
469
|
+
new = self._clone(transform, inherit_kwargs=inherit_kwargs)
|
|
470
|
+
finally:
|
|
471
|
+
# Reset _method for whatever happens after the accessor has been
|
|
472
|
+
# fully resolved, e.g. whatever happens `dfi.A > 1`.
|
|
473
|
+
self._method = None
|
|
474
|
+
return new
|
|
475
|
+
|
|
476
|
+
def __getattribute__(self, name):
|
|
477
|
+
self_dict = super().__getattribute__('__dict__')
|
|
478
|
+
if not self_dict.get('_init'):
|
|
479
|
+
return super().__getattribute__(name)
|
|
480
|
+
|
|
481
|
+
current = self_dict['_current_']
|
|
482
|
+
method = self_dict['_method']
|
|
483
|
+
if method:
|
|
484
|
+
current = getattr(current, method)
|
|
485
|
+
# Getting all the public attributes available on the current object,
|
|
486
|
+
# e.g. `sum`, `head`, etc.
|
|
487
|
+
extras = [d for d in dir(current) if not d.startswith('_')]
|
|
488
|
+
if name in extras and name not in super().__dir__():
|
|
489
|
+
new = self._resolve_accessor()
|
|
490
|
+
# Setting the method name for a potential use later by e.g. an
|
|
491
|
+
# operator or method, as in `dfi.A > 2`. or `dfi.A.max()`
|
|
492
|
+
new._method = name
|
|
493
|
+
try:
|
|
494
|
+
new.__doc__ = getattr(current, name).__doc__
|
|
495
|
+
except Exception:
|
|
496
|
+
pass
|
|
497
|
+
return new
|
|
498
|
+
return super().__getattribute__(name)
|
|
499
|
+
|
|
500
|
+
@staticmethod
|
|
501
|
+
def _get_ax_fn():
|
|
502
|
+
@pn.depends()
|
|
503
|
+
def get_ax():
|
|
504
|
+
from matplotlib.backends.backend_agg import FigureCanvas
|
|
505
|
+
from matplotlib.pyplot import Figure
|
|
506
|
+
|
|
507
|
+
Interactive._fig = fig = Figure()
|
|
508
|
+
FigureCanvas(fig)
|
|
509
|
+
return fig.subplots()
|
|
510
|
+
|
|
511
|
+
return get_ax
|
|
512
|
+
|
|
513
|
+
def __call__(self, *args, **kwargs):
|
|
514
|
+
"""
|
|
515
|
+
The `.interactive` API enhances the API of data analysis libraries
|
|
516
|
+
like Pandas, Dask, and Xarray, by allowing to replace in a pipeline
|
|
517
|
+
static values by dynamic widgets. When displayed, an interactive
|
|
518
|
+
pipeline will incorporate the dynamic widgets that control it, as long
|
|
519
|
+
as its normal output that will automatically be updated as soon as a
|
|
520
|
+
widget value is changed.
|
|
521
|
+
|
|
522
|
+
Reference: https://hvplot.holoviz.org/user_guide/Interactive.html
|
|
523
|
+
|
|
524
|
+
Parameters
|
|
525
|
+
----------
|
|
526
|
+
loc : str, optional
|
|
527
|
+
Widget(s) location, one of 'bottom_left', 'bottom_right', 'right'
|
|
528
|
+
'top-right', 'top-left' and 'left'. By default 'top_left'
|
|
529
|
+
center : bool, optional
|
|
530
|
+
Whether to center to pipeline output, by default False
|
|
531
|
+
max_rows : int, optional
|
|
532
|
+
Maximum number of rows displayed, only used when the output is a
|
|
533
|
+
dataframe, by default 100
|
|
534
|
+
kwargs: optional
|
|
535
|
+
Optional kwargs that are passed down to customize the displayed
|
|
536
|
+
object. E.g. if the output is a DataFrame `width=200` will set
|
|
537
|
+
the size of the DataFrame Pane that renders it.
|
|
538
|
+
|
|
539
|
+
Returns
|
|
540
|
+
-------
|
|
541
|
+
Interactive
|
|
542
|
+
The next `Interactive` object of the pipeline.
|
|
543
|
+
|
|
544
|
+
Examples
|
|
545
|
+
--------
|
|
546
|
+
>>> widget = panel.widgets.IntSlider(value=1, start=1, end=5)
|
|
547
|
+
>>> dfi = df.interactive(width=200)
|
|
548
|
+
>>> dfi.head(widget)
|
|
549
|
+
"""
|
|
550
|
+
|
|
551
|
+
if self._method is None:
|
|
552
|
+
if self._depth == 0:
|
|
553
|
+
# This code path is entered when initializing an interactive
|
|
554
|
+
# class from the accessor, e.g. with df.interactive(). As
|
|
555
|
+
# calling the accessor df.interactive already returns an
|
|
556
|
+
# Interactive instance.
|
|
557
|
+
return self._clone(*args, **kwargs)
|
|
558
|
+
# TODO: When is this error raised?
|
|
559
|
+
raise AttributeError
|
|
560
|
+
elif self._method == 'plot':
|
|
561
|
+
# This - {ax: get_ax} - is passed as kwargs to the plot method in
|
|
562
|
+
# the dim expression.
|
|
563
|
+
kwargs['ax'] = self._get_ax_fn()
|
|
564
|
+
new = self._clone(copy=True)
|
|
565
|
+
try:
|
|
566
|
+
method = type(new._transform)(new._transform, new._method, accessor=True)
|
|
567
|
+
kwargs = dict(new._inherit_kwargs, **kwargs)
|
|
568
|
+
clone = new._clone(method(*args, **kwargs), plot=new._method == 'plot')
|
|
569
|
+
finally:
|
|
570
|
+
# If an error occurs reset _method anyway so that, e.g. the next
|
|
571
|
+
# attempt in a Notebook, is set appropriately.
|
|
572
|
+
new._method = None
|
|
573
|
+
return clone
|
|
574
|
+
|
|
575
|
+
# ----------------------------------------------------------------
|
|
576
|
+
# Interactive pipeline APIs
|
|
577
|
+
# ----------------------------------------------------------------
|
|
578
|
+
|
|
579
|
+
def __array_ufunc__(self, *args, **kwargs):
|
|
580
|
+
# TODO: How to trigger this method?
|
|
581
|
+
new = self._resolve_accessor()
|
|
582
|
+
transform = new._transform
|
|
583
|
+
transform = args[0](transform, *args[3:], **kwargs)
|
|
584
|
+
return new._clone(transform)
|
|
585
|
+
|
|
586
|
+
def _apply_operator(self, operator, *args, reverse=False, **kwargs):
|
|
587
|
+
new = self._resolve_accessor()
|
|
588
|
+
transform = new._transform
|
|
589
|
+
transform = type(transform)(transform, operator, *args, reverse=reverse)
|
|
590
|
+
return new._clone(transform)
|
|
591
|
+
|
|
592
|
+
# Builtin functions
|
|
593
|
+
|
|
594
|
+
def __abs__(self):
|
|
595
|
+
return self._apply_operator(abs)
|
|
596
|
+
|
|
597
|
+
def __round__(self, ndigits=None):
|
|
598
|
+
args = () if ndigits is None else (ndigits,)
|
|
599
|
+
return self._apply_operator(round, *args)
|
|
600
|
+
|
|
601
|
+
# Unary operators
|
|
602
|
+
def __neg__(self):
|
|
603
|
+
return self._apply_operator(operator.neg)
|
|
604
|
+
|
|
605
|
+
def __not__(self):
|
|
606
|
+
return self._apply_operator(operator.not_)
|
|
607
|
+
|
|
608
|
+
def __invert__(self):
|
|
609
|
+
return self._apply_operator(operator.inv)
|
|
610
|
+
|
|
611
|
+
def __pos__(self):
|
|
612
|
+
return self._apply_operator(operator.pos)
|
|
613
|
+
|
|
614
|
+
# Binary operators
|
|
615
|
+
def __add__(self, other):
|
|
616
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
617
|
+
return self._apply_operator(operator.add, other)
|
|
618
|
+
|
|
619
|
+
def __and__(self, other):
|
|
620
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
621
|
+
return self._apply_operator(operator.and_, other)
|
|
622
|
+
|
|
623
|
+
def __eq__(self, other):
|
|
624
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
625
|
+
return self._apply_operator(operator.eq, other)
|
|
626
|
+
|
|
627
|
+
def __floordiv__(self, other):
|
|
628
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
629
|
+
return self._apply_operator(operator.floordiv, other)
|
|
630
|
+
|
|
631
|
+
def __ge__(self, other):
|
|
632
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
633
|
+
return self._apply_operator(operator.ge, other)
|
|
634
|
+
|
|
635
|
+
def __gt__(self, other):
|
|
636
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
637
|
+
return self._apply_operator(operator.gt, other)
|
|
638
|
+
|
|
639
|
+
def __le__(self, other):
|
|
640
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
641
|
+
return self._apply_operator(operator.le, other)
|
|
642
|
+
|
|
643
|
+
def __lt__(self, other):
|
|
644
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
645
|
+
return self._apply_operator(operator.lt, other)
|
|
646
|
+
|
|
647
|
+
def __lshift__(self, other):
|
|
648
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
649
|
+
return self._apply_operator(operator.lshift, other)
|
|
650
|
+
|
|
651
|
+
def __mod__(self, other):
|
|
652
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
653
|
+
return self._apply_operator(operator.mod, other)
|
|
654
|
+
|
|
655
|
+
def __mul__(self, other):
|
|
656
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
657
|
+
return self._apply_operator(operator.mul, other)
|
|
658
|
+
|
|
659
|
+
def __ne__(self, other):
|
|
660
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
661
|
+
return self._apply_operator(operator.ne, other)
|
|
662
|
+
|
|
663
|
+
def __or__(self, other):
|
|
664
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
665
|
+
return self._apply_operator(operator.or_, other)
|
|
666
|
+
|
|
667
|
+
def __rshift__(self, other):
|
|
668
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
669
|
+
return self._apply_operator(operator.rshift, other)
|
|
670
|
+
|
|
671
|
+
def __pow__(self, other):
|
|
672
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
673
|
+
return self._apply_operator(operator.pow, other)
|
|
674
|
+
|
|
675
|
+
def __sub__(self, other):
|
|
676
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
677
|
+
return self._apply_operator(operator.sub, other)
|
|
678
|
+
|
|
679
|
+
def __truediv__(self, other):
|
|
680
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
681
|
+
return self._apply_operator(operator.truediv, other)
|
|
682
|
+
|
|
683
|
+
# Reverse binary operators
|
|
684
|
+
def __radd__(self, other):
|
|
685
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
686
|
+
return self._apply_operator(operator.add, other, reverse=True)
|
|
687
|
+
|
|
688
|
+
def __rand__(self, other):
|
|
689
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
690
|
+
return self._apply_operator(operator.and_, other, reverse=True)
|
|
691
|
+
|
|
692
|
+
def __rdiv__(self, other):
|
|
693
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
694
|
+
return self._apply_operator(operator.div, other, reverse=True)
|
|
695
|
+
|
|
696
|
+
def __rfloordiv__(self, other):
|
|
697
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
698
|
+
return self._apply_operator(operator.floordiv, other, reverse=True)
|
|
699
|
+
|
|
700
|
+
def __rlshift__(self, other):
|
|
701
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
702
|
+
return self._apply_operator(operator.rlshift, other)
|
|
703
|
+
|
|
704
|
+
def __rmod__(self, other):
|
|
705
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
706
|
+
return self._apply_operator(operator.mod, other, reverse=True)
|
|
707
|
+
|
|
708
|
+
def __rmul__(self, other):
|
|
709
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
710
|
+
return self._apply_operator(operator.mul, other, reverse=True)
|
|
711
|
+
|
|
712
|
+
def __ror__(self, other):
|
|
713
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
714
|
+
return self._apply_operator(operator.or_, other, reverse=True)
|
|
715
|
+
|
|
716
|
+
def __rpow__(self, other):
|
|
717
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
718
|
+
return self._apply_operator(operator.pow, other, reverse=True)
|
|
719
|
+
|
|
720
|
+
def __rrshift__(self, other):
|
|
721
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
722
|
+
return self._apply_operator(operator.rrshift, other)
|
|
723
|
+
|
|
724
|
+
def __rsub__(self, other):
|
|
725
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
726
|
+
return self._apply_operator(operator.sub, other, reverse=True)
|
|
727
|
+
|
|
728
|
+
def __rtruediv__(self, other):
|
|
729
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
730
|
+
return self._apply_operator(operator.truediv, other, reverse=True)
|
|
731
|
+
|
|
732
|
+
def __getitem__(self, other):
|
|
733
|
+
other = other._transform if isinstance(other, Interactive) else other
|
|
734
|
+
return self._apply_operator(operator.getitem, other)
|
|
735
|
+
|
|
736
|
+
def _plot(self, *args, **kwargs):
|
|
737
|
+
# TODO: Seems totally unused to me, as self._plot is set to a boolean in __init__
|
|
738
|
+
@pn.depends()
|
|
739
|
+
def get_ax():
|
|
740
|
+
from matplotlib.backends.backend_agg import FigureCanvas
|
|
741
|
+
from matplotlib.pyplot import Figure
|
|
742
|
+
|
|
743
|
+
Interactive._fig = fig = Figure()
|
|
744
|
+
FigureCanvas(fig)
|
|
745
|
+
return fig.subplots()
|
|
746
|
+
|
|
747
|
+
kwargs['ax'] = get_ax
|
|
748
|
+
new = self._resolve_accessor()
|
|
749
|
+
transform = new._transform
|
|
750
|
+
transform = type(transform)(transform, 'plot', accessor=True)
|
|
751
|
+
return new._clone(transform(*args, **kwargs), plot=True)
|
|
752
|
+
|
|
753
|
+
# ----------------------------------------------------------------
|
|
754
|
+
# Public API
|
|
755
|
+
# ----------------------------------------------------------------
|
|
756
|
+
|
|
757
|
+
def dmap(self):
|
|
758
|
+
"""
|
|
759
|
+
Wraps the output in a DynamicMap. Only valid if the output
|
|
760
|
+
is a HoloViews object.
|
|
761
|
+
"""
|
|
762
|
+
return hv.DynamicMap(self._callback)
|
|
763
|
+
|
|
764
|
+
def eval(self):
|
|
765
|
+
"""
|
|
766
|
+
Returns the current state of the interactive expression. The
|
|
767
|
+
returned object is no longer interactive.
|
|
768
|
+
"""
|
|
769
|
+
if self._dirty:
|
|
770
|
+
obj = self._obj
|
|
771
|
+
ds = hv.Dataset(_convert_col_names_to_str(obj))
|
|
772
|
+
transform = self._transform
|
|
773
|
+
if ds.interface.datatype == 'xarray' and is_xarray_dataarray(obj):
|
|
774
|
+
transform = transform.clone(obj.name)
|
|
775
|
+
obj = transform.apply(ds, keep_index=True, compute=False)
|
|
776
|
+
self._current_ = obj
|
|
777
|
+
self._dirty = False
|
|
778
|
+
else:
|
|
779
|
+
obj = self._current_
|
|
780
|
+
|
|
781
|
+
if self._method:
|
|
782
|
+
# E.g. `pi = dfi.A` leads to `pi._method` equal to `'A'`.
|
|
783
|
+
obj = getattr(obj, self._method, obj)
|
|
784
|
+
if self._plot:
|
|
785
|
+
obj = Interactive._fig
|
|
786
|
+
|
|
787
|
+
return obj
|
|
788
|
+
|
|
789
|
+
def layout(self, **kwargs):
|
|
790
|
+
"""
|
|
791
|
+
Returns a layout of the widgets and output arranged according
|
|
792
|
+
to the center and widget location specified in the
|
|
793
|
+
interactive call.
|
|
794
|
+
"""
|
|
795
|
+
if bokeh3:
|
|
796
|
+
return self._layout_bk3(**kwargs)
|
|
797
|
+
return self._layout_bk2(**kwargs)
|
|
798
|
+
|
|
799
|
+
def _layout_bk2(self, **kwargs):
|
|
800
|
+
widget_box = self.widgets()
|
|
801
|
+
panel = self.output()
|
|
802
|
+
loc = self._loc
|
|
803
|
+
if loc in ('left', 'right'):
|
|
804
|
+
widgets = Column(VSpacer(), widget_box, VSpacer())
|
|
805
|
+
elif loc in ('top', 'bottom'):
|
|
806
|
+
widgets = Row(HSpacer(), widget_box, HSpacer())
|
|
807
|
+
elif loc in ('top_left', 'bottom_left'):
|
|
808
|
+
widgets = Row(widget_box, HSpacer())
|
|
809
|
+
elif loc in ('top_right', 'bottom_right'):
|
|
810
|
+
widgets = Row(HSpacer(), widget_box)
|
|
811
|
+
elif loc in ('left_top', 'right_top'):
|
|
812
|
+
widgets = Column(widget_box, VSpacer())
|
|
813
|
+
elif loc in ('left_bottom', 'right_bottom'):
|
|
814
|
+
widgets = Column(VSpacer(), widget_box)
|
|
815
|
+
# TODO: add else and raise error
|
|
816
|
+
center = self._center
|
|
817
|
+
if not widgets:
|
|
818
|
+
if center:
|
|
819
|
+
components = [HSpacer(), panel, HSpacer()]
|
|
820
|
+
else:
|
|
821
|
+
components = [panel]
|
|
822
|
+
elif center:
|
|
823
|
+
if loc.startswith('left'):
|
|
824
|
+
components = [widgets, HSpacer(), panel, HSpacer()]
|
|
825
|
+
elif loc.startswith('right'):
|
|
826
|
+
components = [HSpacer(), panel, HSpacer(), widgets]
|
|
827
|
+
elif loc.startswith('top'):
|
|
828
|
+
components = [
|
|
829
|
+
HSpacer(),
|
|
830
|
+
Column(widgets, Row(HSpacer(), panel, HSpacer())),
|
|
831
|
+
HSpacer(),
|
|
832
|
+
]
|
|
833
|
+
elif loc.startswith('bottom'):
|
|
834
|
+
components = [
|
|
835
|
+
HSpacer(),
|
|
836
|
+
Column(Row(HSpacer(), panel, HSpacer()), widgets),
|
|
837
|
+
HSpacer(),
|
|
838
|
+
]
|
|
839
|
+
else:
|
|
840
|
+
if loc.startswith('left'):
|
|
841
|
+
components = [widgets, panel]
|
|
842
|
+
elif loc.startswith('right'):
|
|
843
|
+
components = [panel, widgets]
|
|
844
|
+
elif loc.startswith('top'):
|
|
845
|
+
components = [Column(widgets, panel)]
|
|
846
|
+
elif loc.startswith('bottom'):
|
|
847
|
+
components = [Column(panel, widgets)]
|
|
848
|
+
return Row(*components, **kwargs)
|
|
849
|
+
|
|
850
|
+
def _layout_bk3(self, **kwargs):
|
|
851
|
+
widget_box = self.widgets()
|
|
852
|
+
panel = self.output()
|
|
853
|
+
loc = self._loc
|
|
854
|
+
center = self._center
|
|
855
|
+
alignments = {
|
|
856
|
+
'left': (Row, ('start', 'center'), True),
|
|
857
|
+
'right': (Row, ('end', 'center'), False),
|
|
858
|
+
'top': (Column, ('center', 'start'), True),
|
|
859
|
+
'bottom': (Column, ('center', 'end'), False),
|
|
860
|
+
'top_left': (Column, 'start', True),
|
|
861
|
+
'top_right': (Column, ('end', 'start'), True),
|
|
862
|
+
'bottom_left': (Column, ('start', 'end'), False),
|
|
863
|
+
'bottom_right': (Column, 'end', False),
|
|
864
|
+
'left_top': (Row, 'start', True),
|
|
865
|
+
'left_bottom': (Row, ('start', 'end'), True),
|
|
866
|
+
'right_top': (Row, ('end', 'start'), False),
|
|
867
|
+
'right_bottom': (Row, 'end', False),
|
|
868
|
+
}
|
|
869
|
+
layout, align, widget_first = alignments[loc]
|
|
870
|
+
widget_box.align = align
|
|
871
|
+
if not len(widget_box):
|
|
872
|
+
if center:
|
|
873
|
+
components = [HSpacer(), panel, HSpacer()]
|
|
874
|
+
else:
|
|
875
|
+
components = [panel]
|
|
876
|
+
return Row(*components, **kwargs)
|
|
877
|
+
|
|
878
|
+
items = (widget_box, panel) if widget_first else (panel, widget_box)
|
|
879
|
+
sizing_mode = kwargs.get('sizing_mode')
|
|
880
|
+
if not center:
|
|
881
|
+
if layout is Row:
|
|
882
|
+
components = list(items)
|
|
883
|
+
else:
|
|
884
|
+
components = [layout(*items, sizing_mode=sizing_mode)]
|
|
885
|
+
elif layout is Column:
|
|
886
|
+
components = [HSpacer(), layout(*items, sizing_mode=sizing_mode), HSpacer()]
|
|
887
|
+
elif loc.startswith('left'):
|
|
888
|
+
components = [widget_box, HSpacer(), panel, HSpacer()]
|
|
889
|
+
else:
|
|
890
|
+
components = [HSpacer(), panel, HSpacer(), widget_box]
|
|
891
|
+
return Row(*components, **kwargs)
|
|
892
|
+
|
|
893
|
+
def holoviews(self):
|
|
894
|
+
"""
|
|
895
|
+
Returns a HoloViews object to render the output of this
|
|
896
|
+
pipeline. Only works if the output of this pipeline is a
|
|
897
|
+
HoloViews object, e.g. from an .hvplot call.
|
|
898
|
+
"""
|
|
899
|
+
return hv.DynamicMap(self._callback)
|
|
900
|
+
|
|
901
|
+
def output(self):
|
|
902
|
+
"""
|
|
903
|
+
Returns the output of the interactive pipeline, which is
|
|
904
|
+
either a HoloViews DynamicMap or a Panel object.
|
|
905
|
+
|
|
906
|
+
Returns
|
|
907
|
+
-------
|
|
908
|
+
DynamicMap or Panel object wrapping the interactive output.
|
|
909
|
+
"""
|
|
910
|
+
return self.holoviews() if self._dmap else self.panel(**self._kwargs)
|
|
911
|
+
|
|
912
|
+
def panel(self, **kwargs):
|
|
913
|
+
"""
|
|
914
|
+
Wraps the output in a Panel component.
|
|
915
|
+
"""
|
|
916
|
+
return pn.panel(self._callback, **kwargs)
|
|
917
|
+
|
|
918
|
+
def widgets(self):
|
|
919
|
+
"""
|
|
920
|
+
Returns a Column of widgets which control the interactive output.
|
|
921
|
+
|
|
922
|
+
Returns
|
|
923
|
+
-------
|
|
924
|
+
A Column of widgets
|
|
925
|
+
"""
|
|
926
|
+
widgets = []
|
|
927
|
+
for p in self._fn_params:
|
|
928
|
+
if isinstance(p.owner, pn.widgets.Widget) and p.owner not in widgets:
|
|
929
|
+
widgets.append(p.owner)
|
|
930
|
+
for op in self._transform.ops:
|
|
931
|
+
for w in _find_widgets(op):
|
|
932
|
+
if w not in widgets:
|
|
933
|
+
widgets.append(w)
|
|
934
|
+
return pn.Column(*widgets)
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
class _hvplot:
|
|
938
|
+
_kinds = tuple(HoloViewsConverter._kind_mapping)
|
|
939
|
+
|
|
940
|
+
__slots__ = ['_interactive']
|
|
941
|
+
|
|
942
|
+
def __init__(self, _interactive):
|
|
943
|
+
self._interactive = _interactive
|
|
944
|
+
|
|
945
|
+
def __call__(self, *args, _kind=None, **kwargs):
|
|
946
|
+
# The underscore in _kind is to not overwrite it
|
|
947
|
+
# if 'kind' is in kwargs and the function
|
|
948
|
+
# is used with partial.
|
|
949
|
+
if _kind and 'kind' in kwargs:
|
|
950
|
+
raise TypeError(f"{_kind}() got an unexpected keyword argument 'kind'")
|
|
951
|
+
if _kind:
|
|
952
|
+
kwargs['kind'] = _kind
|
|
953
|
+
|
|
954
|
+
new = self._interactive._resolve_accessor()
|
|
955
|
+
transform = new._transform
|
|
956
|
+
transform = type(transform)(transform, 'hvplot', accessor=True)
|
|
957
|
+
dmap = 'kind' not in kwargs or isinstance(kwargs['kind'], str)
|
|
958
|
+
return new._clone(transform(*args, **kwargs), dmap=dmap)
|
|
959
|
+
|
|
960
|
+
def __getattr__(self, attr):
|
|
961
|
+
if attr in self._kinds:
|
|
962
|
+
return partial(self, _kind=attr)
|
|
963
|
+
else:
|
|
964
|
+
raise AttributeError(f"'hvplot' object has no attribute '{attr}'")
|
|
965
|
+
|
|
966
|
+
def __dir__(self):
|
|
967
|
+
# This function is for autocompletion
|
|
968
|
+
return self._interactive._obj.hvplot.__all__
|