Skip to content

Make Bound Functions and Parameters work the same. #6543

@MarcSkovMadsen

Description

@MarcSkovMadsen

panel==1.4.0rc3

In Panel we are promoting the concept of references. And references includes bound functions and parameters.

For example in the tutorial Intermediate Interactivity we promote bound functions and parameters as substitutes for each.

But in practice they are treated very differently by pn.panel and hv.DynamicMap as you can see from below example.

This makes it hard to create a mental model of what to expect. This makes it hard to transition from one approach to other approach.

Please make the two approaches equivalent.


On the left you see a bound function. On the right a Parameter.

image

import panel as pn
import param
import pandas as pd
import numpy as np
import hvplot.pandas
import holoviews as hv

pn.extension()


class Data(param.Parameterized):
    value = param.DataFrame()

    submit = param.Event()

    @pn.depends("submit", watch=True, on_init=True)
    def _update_value(self):
        x = np.random.rand(10)
        y = np.random.rand(10)
        self.value = pd.DataFrame({"x": x, "y": y})


class BoundStorage(param.Parameterized):
    data = param.ClassSelector(class_=Data, allow_refs=True)

    @pn.depends("data.value")
    def plot(self):
        return self.data.value.hvplot(x="x", y="y")


class WatchStorage(param.Parameterized):
    data = param.ClassSelector(class_=Data, allow_refs=True)

    plot = param.Parameter()

    @pn.depends("data.value", watch=True, on_init=True)
    def _update_plot(self):
        self.plot = self.data.value.hvplot(x="x", y="y")


data = Data()

bound_view = BoundStorage(data=data)
watch_view = WatchStorage(data=data)


pn.Column(
    data.param.submit,
    "## pn.panel",
    pn.Row(bound_view.plot, watch_view.param.plot),
    "## pn.pane.HoloViews",
    pn.Row(
        pn.pane.HoloViews(bound_view.plot), pn.pane.HoloViews(watch_view.param.plot)
    ),
    "## pn.pane.HoloViews and hv.DynamicMap",
    pn.Row(
        pn.pane.HoloViews(hv.DynamicMap(bound_view.plot)),
        "ValueError: Callable parameter 'Callable.callable' only takes a callable object, not objects of <class 'param.parameterized.Parameter'>.",
    ),  # pn.pane.HoloViews(hv.DynamicMap(watch_view.param.plot))),
).servable()

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiRelated to Panel's APItype: discussionRequiring community discussionwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions