Understanding ParamMethod

Env: bokeh 1.4.0, panel 0.8.0, param 1.9.3

This is the code I’m running:

import param
import panel as pn

class Dummy(param.Parameterized):
    l = param.Number(default=10, bounds=(2,50), step=1)
    x = param.Number(default=2*l.default, bounds=(2, None), step=1)
    
    @param.depends("l", watch=True)
    def update_x(self):
        print("update_x")
        self.x = 2 * self.l
    
    def view(self):
        print("view")
        return self.x

dummy = Dummy()
app = pn.Row(dummy.param.l, dummy.view)
app.servable()

I get this in the standard output when I move the slider twice:

update_x
view
view
update_x
view
view

The view method, which in the app object appears as a ParamMethod object, gets called twice.

FYI, I found a very useful dashboard example on example.pyviz.org (here) that explains how to set up a panel output with dynamic behaviour, I’m trying to understand how the 4 described methods work.

What I’m trying to do is close to Method 1 (Method dependency for Panel). To get it as described, I need to add a @param.depends("paramname") on the view method, such as:

import param
import panel as pn

class Dummy(param.Parameterized):
    l = param.Number(default=10, bounds=(2,50), step=1)
    x = param.Number(default=2*l.default, bounds=(2, None), step=1)
    
    @param.depends("l", watch=True)
    def update_x(self):
        print("update_x")
        self.x = 2 * self.l
    
    @param.depends("x")
    def view(self):
        print("view")
        return self.x

dummy = Dummy()
app = pn.Row(dummy.param.l, dummy.view)
app.servable()

This works better:

update_x
view
update_x
view

But I would be interested to understand why the first test “failed” (view being called twice in a row). It was not obvious that it was not working 100% because the x value was being updated and displayed correctly.

Thanks for your help :slight_smile:

Ok I might have found the answer. The docstring of the ParamMethod class is the following:

"""
    ParamMethod panes wrap methods on parameterized classes and
    rerenders the plot when any of the method's parameters change. By
    default ParamMethod will watch all parameters on the class owning
    the method or can be restricted to certain parameters by annotating
    the method using the param.depends decorator. The method may
    return any object which itself can be rendered as a Pane.
    """

So by default, a ParamMethod will watch all parameters on the class owning the method. In my first toy example, dummy.view was converted as a ParamMethod that was watching l and x. Moving the slider modified l and updated x, thus triggering dummy.view twice. As per the docstring, annotating the view method with param.depends("x") was the right thing to do to force the view method to be triggered only once in that case.

Note to myself, always have a look at the source code :wink:

1 Like

Hi @Maximit

Great you found out by your self. I’m having similar problems all the time.

I think the problems comes from the fact that Panel actually has a lot of functionality, can be used in many different ways and builds on a lot of other powerfull stuff like bokeh, holoviews and param. So there is a lot to learn. But the documentation is actually great. But there is a lot so it also takes time to learn how to navigate it.

But it also means that if you invest the time you will get a lot of powerfull features at hand.

Good luck on the journey. And keep posting both here and on https://github.com/holoviz/panel. It’s really, really important for @philippjfr and the rest of the contributers to get feedback and learn about friction and pain points. Especially from new people coming to Panel.

Thanks.

1 Like

and @maximlt. Great to see you are using the param.Parameterized api (or ways of using Panel). I think that is one of the super powers of Panel. It took some time to get my head round though.

Yes I also find the documentation great and exhaustive. bokeh and holoviews are their own beast and it’s true that when things go wrong it’s not so easy to find where exactly and from which library the problem comes from without understanding very well the whole holoviz ecosystem. But it’s fun anyway :slight_smile:

1 Like