RecursionError in `param` class instantiation

Hi everyone, I was just struggling for a couple of hours with a RecursionError in the my param class. I thought I’d be interesting to share my experience. :slight_smile: In the following code you can find a minimal working example showing which fails to work:

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

df_all = pd.DataFrame(data=np.random.normal(size=100).cumsum(),columns=['col1'])

class Flex_datas(param.Parameterized):
    df = param.DataFrame(default=df_all)
    col1 = pn.widgets.RangeSlider(name='col1',start=df_all.col1.min(),end=df_all.col1.max())
    
    def __init__(self,**params):
        super().__init__(**params)
        self.layout = pn.Row(self.col1,self.view)
    
    @pn.depends('col1')
    def view(self):
        querystring = f'col1>{self.col1.value[0]} & col1<{self.col1.value[1]}'
        self.df = df_all.query(querystring)
        return hv.Layout([self.df.hvplot.scatter(y=col,label=col) for col in self.df.columns])
f = Flex_datas()
f.layout

image

Eventually, by constructing the minimal example above, I was able to figure out what was going on…

2 Likes

The solution is in the @pn.depends('col1') line.
The code seems to accept the pn.widget.RangeSlider name as argument, but it ignores the line entirely. As a consequence I guess the default behaviour for param methods without a @pn.depends decorator is active: the method gets called whenever any parameter of the class changes, which in my code is the update of self.df, hence infinite recursion.

Since col1 is a panel widget and not a param.Parameter, what I should’ve written is:

@pn.depends('col1.value')
def view(self)
 ...

Now it works:

But in general I would expect @pn.depends('col1') throws an error, similarly to what it does when using other incorrect arguments. Is there a reason to accept widget objects?
And even if col1 updates are ignored, I would expect that adding @pn.depends('col1') prevents the default param method behavior: being called when any parameter changes?

Kind regards!