Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot link tap to float slider #6384

Open
ahuang11 opened this issue Sep 23, 2024 · 3 comments · May be fixed by #6396
Open

Cannot link tap to float slider #6384

ahuang11 opened this issue Sep 23, 2024 · 3 comments · May be fixed by #6396
Labels
type: enhancement Minor feature or improvement to an existing feature

Comments

@ahuang11
Copy link
Collaborator

ahuang11 commented Sep 23, 2024

For widgets, there’s pn.Param and from_param.

What’s the equivalent for HoloViews streams?

https://discourse.holoviz.org/t/whats-the-best-practice-for-linking-up-holoviews-streams-with-parameterized/8215/3

I thought the following would work, but I get TypeError: Constant parameter 'x' cannot be modified

Can we remove the constant=True, or create a dedicated method like from_param or from_params?

image
import param
import holoviews as hv
import panel as pn
pn.extension()

class TapTest(pn.custom.PyComponent):
    x = param.Number(default=0)
    y = param.Number(default=0)

    def __init__(self, **params):
        super().__init__(**params)

        self._tap = hv.streams.Tap()

        hv_pane = pn.pane.HoloViews()
        hv_pane.object = hv.DynamicMap(self.plot, streams=[self._tap])

        x_spinner = pn.widgets.FloatSlider.from_param(self.param.x)
        y_spinner = pn.widgets.FloatSlider.from_param(self.param.y)
        x_spinner.link(self._tap, value='x', bidirectional=True)
        y_spinner.link(self._tap, value='y', bidirectional=True)
        self._layout = pn.Column(hv_pane, x_spinner, y_spinner)


    def plot(self, x, y):
        return hv.Points([(x, y)]).opts(tools=['tap'])

    def __panel__(self):
        return self._layout

pn.serve(TapTest())

The workaround is a bit more code

import param
import holoviews as hv
import panel as pn
pn.extension()

class TapTest(pn.custom.PyComponent):
    x = param.Number(default=0)
    y = param.Number(default=0)

    def __init__(self, **params):
        super().__init__(**params)

        self._tap = hv.streams.Tap()
        hv_pane = pn.pane.HoloViews()
        hv_pane.object = hv.DynamicMap(self.plot, streams=[self._tap])
        
        pn.bind(self.update_tap, self.param.x, self.param.y, watch=True)
        
        self._layout = pn.Column(hv_pane, self.param.x, self.param.y)

    def update_tap(self, x, y):
        self._tap.event(x=x, y=y)

    def plot(self, x, y):
        if x and y:
            self.param.update(x=x, y=y)
        return hv.Points([(self.x, self.y)]).opts(tools=['tap'])

    def __panel__(self):
        return self._layout

pn.serve(TapTest())
@ahuang11 ahuang11 added TRIAGE Needs triaging type: enhancement Minor feature or improvement to an existing feature and removed TRIAGE Needs triaging labels Sep 23, 2024
@philippjfr
Copy link
Member

This issue has been mentioned on HoloViz Discourse. There might be relevant details there:

https://discourse.holoviz.org/t/whats-the-best-practice-for-linking-up-holoviews-streams-with-parameterized/8215/4

@philippjfr
Copy link
Member

Fully in favor.

@ahuang11 ahuang11 linked a pull request Oct 9, 2024 that will close this issue
@ahuang11
Copy link
Collaborator Author

ahuang11 commented Oct 9, 2024

Not sure how to link the params:

import param
import holoviews as hv
import panel as pn

pn.extension()

class Test(pn.custom.PyComponent):

    x = param.Number(default=1, allow_refs=True)
    y = param.Number(default=0.5, allow_refs=True)

    def __init__(self, **params):
        super().__init__(**params)

        self._tap = hv.streams.Tap.from_param(x=self.param.x, y=self.param.y)
        hv_pane = pn.pane.HoloViews(object=hv.DynamicMap(self.plot, streams=[self._tap]))
        self._layout = pn.Column(hv_pane, self.param.x, self.param.y)

    def plot(self, x, y):
        return hv.Points([(x, y)]).opts(tools=['tap'], xlim=(0, 2), ylim=(0, 2))


    def __panel__(self):
        return self._layout

test = Test()
test

Under the hood changes:

class Tap(PointerXY):
    """
    The x/y-position of a tap or click in data coordinates.
    """
    x = param.ClassSelector(class_=pointer_types, default=None,
                            constant=False, allow_refs=True, doc="""
           Pointer position along the x-axis in data coordinates""")

    y = param.ClassSelector(class_=pointer_types, default=None,
                            constant=False, allow_refs=True, doc="""
           Pointer position along the y-axis in data coordinates""")

    @classmethod
    def from_param(cls, x=None, y=None):
        tap = cls(x=x, y=y)
        tap._x_parameterized = x.owner if isinstance(x, param.Parameter) else None
        tap._y_parameterized = y.owner if isinstance(y, param.Parameter) else None
        tap.param.watch(tap._event, ["x", "y"])
        return tap

    def _event(self, *events):
        print(self.x, self.y)
        self._x_parameterized.x = self.param.x
        self._y_parameterized.y = self.param.y
        self.trigger([self])

It works fine, until tapping... then it unsyncs the widget when tapped.

Screen.Recording.2024-10-09.at.2.49.04.PM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants