Skip to content

Commit

Permalink
Bump minimum version of param to 2.0 and add stream transform (#6230)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored May 14, 2024
1 parent cbd8e0c commit 6f042b3
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 55 deletions.
11 changes: 6 additions & 5 deletions holoviews/core/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Module for accessor objects for viewable HoloViews objects.
"""
import copy
import sys
from functools import wraps
from types import FunctionType

Expand Down Expand Up @@ -133,6 +132,8 @@ def __call__(self, apply_function, streams=None, link_inputs=True,
A new object where the function was applied to all
contained (Nd)Overlay or Element objects.
"""
from panel.widgets.base import Widget

from ..util import Dynamic
from .data import Dataset
from .dimension import ViewableElement
Expand Down Expand Up @@ -168,10 +169,10 @@ def apply_function(object, **kwargs):
'method exists on the object.')
return method(*args, **kwargs)

if 'panel' in sys.modules:
from panel.widgets.base import Widget
kwargs = {k: v.param.value if isinstance(v, Widget) else v
for k, v in kwargs.items()}
kwargs = {
k: v.param.value if isinstance(v, Widget) else v
for k, v in kwargs.items()
}

spec = Element if per_element else ViewableElement
applies = isinstance(self._obj, spec)
Expand Down
8 changes: 4 additions & 4 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ class Callable(param.Parameterized):
information see the DynamicMap tutorial at holoviews.org.
"""

callable = param.Callable(default=None, constant=True, doc="""
The callable function being wrapped.""", **util.disallow_refs)
callable = param.Callable(default=None, constant=True, allow_refs=False, doc="""
The callable function being wrapped.""")

inputs = param.List(default=[], constant=True, doc="""
The list of inputs the callable function is wrapping. Used
Expand Down Expand Up @@ -546,7 +546,7 @@ def __call__(self, *args, **kwargs):
# Nothing to do for callbacks that accept no arguments
kwarg_hash = kwargs.pop('_memoization_hash_', ())
(self.args, self.kwargs) = (args, kwargs)
if util.param_version >= util.Version('2.0.0') and isinstance(self.callable, param.rx):
if isinstance(self.callable, param.rx):
return self.callable.rx.value
elif not args and not kwargs and not any(kwarg_hash):
return self.callable()
Expand Down Expand Up @@ -774,7 +774,7 @@ def __init__(self, callback, initial_items=None, streams=None, **params):
streams = streams_list_from_dict(streams)

# If callback is a parameterized method and watch is disabled add as stream
if util.param_version > util.Version('2.0.0rc1') and param.parameterized.resolve_ref(callback):
if param.parameterized.resolve_ref(callback):
streams.append(callback)
elif (params.get('watch', True) and (util.is_param_method(callback, has_deps=True) or
(isinstance(callback, FunctionType) and hasattr(callback, '_dinfo')))):
Expand Down
14 changes: 4 additions & 10 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@

anonymous_dimension_label = '_'

disallow_refs = {'allow_refs': False} if param_version > Version('2.0.0rc1') else {}

# Argspec was removed in Python 3.11
ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')

Expand Down Expand Up @@ -1613,6 +1611,8 @@ def resolve_dependent_value(value):
A new value where any parameter dependencies have been
resolved.
"""
from panel.widgets import RangeSlider

range_widget = False
if isinstance(value, list):
value = [resolve_dependent_value(v) for v in value]
Expand All @@ -1629,14 +1629,8 @@ def resolve_dependent_value(value):
resolve_dependent_value(value.step),
)

if 'panel' in sys.modules:
from panel.depends import param_value_if_widget
from panel.widgets import RangeSlider
range_widget = isinstance(value, RangeSlider)
if param_version > Version('2.0.0rc1'):
value = param.parameterized.resolve_value(value)
else:
value = param_value_if_widget(value)
range_widget = isinstance(value, RangeSlider)
value = param.parameterized.resolve_value(value)

if is_param_method(value, has_deps=True):
value = value()
Expand Down
30 changes: 13 additions & 17 deletions holoviews/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
server-side or in Javascript in the Jupyter notebook (client-side).
"""

import sys
import weakref
from collections import defaultdict
from contextlib import contextmanager
Expand All @@ -16,7 +15,6 @@
import numpy as np
import pandas as pd
import param
from packaging.version import Version

from .core import util
from .core.ndmapping import UniformNdMapping
Expand Down Expand Up @@ -49,12 +47,7 @@ def streams_list_from_dict(streams):
"Converts a streams dictionary into a streams list"
params = {}
for k, v in streams.items():
if 'panel' in sys.modules:
if util.param_version > util.Version('2.0.0rc1'):
v = param.parameterized.transform_reference(v)
else:
from panel.depends import param_value_if_widget
v = param_value_if_widget(v)
v = param.parameterized.transform_reference(v)
if isinstance(v, param.Parameter) and v.owner is not None:
params[k] = v
else:
Expand Down Expand Up @@ -225,10 +218,7 @@ def _process_streams(cls, streams):
rename = {(p.owner, p.name): k for k, p in deps.get('kw', {}).items()}
s = Params(parameters=dep_params, rename=rename)
else:
if util.param_version > util.Version('2.0.0rc1'):
deps = param.parameterized.resolve_ref(s)
else:
deps = None
deps = param.parameterized.resolve_ref(s)
if deps:
s = Params(parameters=deps)
else:
Expand Down Expand Up @@ -689,16 +679,13 @@ class Params(Stream):

parameterized = param.ClassSelector(class_=(param.Parameterized,
param.parameterized.ParameterizedMetaclass),
constant=True, allow_None=True, doc="""
Parameterized instance to watch for parameter changes.""", **util.disallow_refs)
constant=True, allow_None=True, allow_refs=False, doc="""
Parameterized instance to watch for parameter changes.""")

parameters = param.List(default=[], constant=True, doc="""
Parameters on the parameterized to watch.""")

def __init__(self, parameterized=None, parameters=None, watch=True, watch_only=False, **params):
if util.param_version < Version('1.8.0') and watch:
raise RuntimeError('Params stream requires param version >= 1.8.0, '
'to support watching parameters.')
if parameters is None:
parameters = [parameterized.param[p] for p in parameterized.param if p != 'name']
else:
Expand Down Expand Up @@ -1907,3 +1894,12 @@ def __init__(self, vertex_style=None, shared=True, **params):
vertex_style = {}
self.shared = shared
super().__init__(vertex_style=vertex_style, **params)


def _streams_transform(obj):
if isinstance(obj, Pipe):
return obj.param.data
return obj


param.reactive.register_reference_transform(_streams_transform)
12 changes: 6 additions & 6 deletions holoviews/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,8 @@ class Dynamic(param.ParameterizedFunction):
shared_data = param.Boolean(default=False, doc="""
Whether the cloned DynamicMap will share the same cache.""")

streams = param.ClassSelector(default=[], class_=(list, dict), doc="""
List of streams to attach to the returned DynamicMap""", **util.disallow_refs)
streams = param.ClassSelector(default=[], class_=(list, dict), allow_refs=False, doc="""
List of streams to attach to the returned DynamicMap""")

def __call__(self, map_obj, **params):
watch = params.pop('watch', True)
Expand Down Expand Up @@ -921,6 +921,8 @@ def _get_streams(self, map_obj, watch=True):
of supplied stream classes and instances are processed and
added to the list.
"""
from panel.widgets.base import Widget

if isinstance(self.p.streams, dict):
streams = defaultdict(dict)
stream_specs, params = [], {}
Expand Down Expand Up @@ -961,10 +963,8 @@ def _get_streams(self, map_obj, watch=True):

params = {}
for k, v in self.p.kwargs.items():
if 'panel' in sys.modules:
from panel.widgets.base import Widget
if isinstance(v, Widget):
v = v.param.value
if isinstance(v, Widget):
v = v.param.value
if isinstance(v, param.Parameter) and isinstance(v.owner, param.Parameterized):
params[k] = v
streams += Params.from_params(params)
Expand Down
16 changes: 5 additions & 11 deletions holoviews/util/transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import operator
import sys
from types import BuiltinFunctionType, BuiltinMethodType, FunctionType, MethodType

import numpy as np
Expand Down Expand Up @@ -360,11 +359,7 @@ def register(cls, key, function):

@property
def params(self):
if 'panel' in sys.modules:
from panel.widgets.base import Widget
else:
Widget = None

from panel.widgets.base import Widget
params = {}
for op in self.ops:
op_args = list(op['args'])+list(op['kwargs'].values())
Expand All @@ -373,18 +368,17 @@ def params(self):
op_args += [op['fn'].index]
op_args = flatten(op_args)
for op_arg in op_args:
if Widget and isinstance(op_arg, Widget):
if isinstance(op_arg, Widget):
op_arg = op_arg.param.value
if isinstance(op_arg, dim):
params.update(op_arg.params)
elif isinstance(op_arg, slice):
(start, stop, step) = (op_arg.start, op_arg.stop, op_arg.step)

if Widget and isinstance(start, Widget):
if isinstance(start, Widget):
start = start.param.value
if Widget and isinstance(stop, Widget):
if isinstance(stop, Widget):
stop = stop.param.value
if Widget and isinstance(step, Widget):
if isinstance(step, Widget):
step = step.param.value

if isinstance(start, param.Parameter):
Expand Down
2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ numpy = ">=1.0"
packaging = "*"
pandas = ">=0.20.0"
panel = ">=1.0"
param = ">=1.12.0,<3.0"
param = ">=2.0,<3.0"
pip = "*"
pyviz_comms = ">=2.1"
# Recommended
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
"Topic :: Software Development :: Libraries",
]
dependencies = [
"param >=1.12.0,<3.0",
"param >=2.0,<3.0",
"numpy >=1.0",
"pyviz_comms >=2.1",
"panel >=1.0",
Expand Down

0 comments on commit 6f042b3

Please sign in to comment.