@pn.depends on non-method function

Hi,

I am dynamically generating DynamicMaps plots inside a Parametrized class method, like this:

    def create_nolay_plot(self, ind, associated_plots, streams):

        def make_f(self, indic):
            def f(x_range, y_range):
                return hv.Curve(self.data_df).opts(**indic.plot_opts)
            return f

        f_dyn = make_f(ind)
        dp = hv.DynamicMap(f_dyn, streams=streams).opts(responsive=True,
                                                              xticks=30, active_tools=["wheel_zoom"],
                                                              show_grid=True)

I’d like to be able to add @pn.depends() to the f function, but as it is not a class method, I don’t know how to do.

What I want :

def create_nolay_plot(self, ind, associated_plots, streams):
         def make_f(self, indic):
            @pn.depends("interval")
            def f(x_range, y_range):
                return hv.Curve(self.data_df).opts(**indic.plot_opts)
            return f

Thanks !

1 Like

Hi @Skealz

Could you please create a minimum, reproducible example that can (almost) run. It is simply much easier to work with. Right now I have to guess what streams etc. is.

Thanks.

I successfully achieved it with this :

class P(param.Parametrized):
     interval = param.ObjectSelector(default=60, objects=[1, 30, 60, 1440])

     def __init__(self, **params):
          super().__init__(**params)
          self.interval_widget = pn.Param(self.param.interval)[0]

    def create_nolay_plot(self, ind, associated_plots, streams):

        def make_f(indic):
            @pn.depends(interval=self.interval_widget.param.value, watch=True)
            def f(x_range=None, y_range=None, interval=None):
                return indic.to_plot(self.data_df).opts(**indic.plot_opts)

            return f
       
        f_dyn = make_f(ind)
        dp = hv.DynamicMap(f_dyn,...)
2 Likes