Resetable Button Extension Makes Interactivity Buggy

Consider the following…

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

from bokeh.palettes import Turbo256, Category20


class ResettableParameterized(pm.Parameterized):
    def _reset(self):
        with pm.parameterized.batch_call_watchers(self):
            for param in self.param:
                if param not in ["name"]:
                    setattr(self, param, self.param[param].default)

    def panel(self):
        """Generate a Panel layout including parameter controls and a reset button."""
        reset_button = pn.widgets.Button(name="Reset to Defaults", button_type="warning")
        reset_button.on_click(lambda event: self._reset())
        return pn.Column(reset_button, pn.Param(self.param))

class Test(ResettableParameterized):
    y = pm.Number(10, bounds=(0,20))
    def view(self):
        xs = np.linspace(0,1,100)
        ys = [self.y for x in xs]
        df = pd.DataFrame({'x':xs,'y':ys})
        return df.hvplot(x='x',y='y')

t = Test()

pn.Row(t.panel, t.view)

My intention is to create a Parameterized base class that gives a ‘reset to defaults’ button to all parameterized classes. However, when I implement in the case above, my interactive model becomes buggy and unusable. I’m not sure what’s happening to cause the bug.

Any insights or tips for refactoring?

Try: setattr(self, param, deepcopy(self.param[param].default)) could be mutable state is shared. copy could maybe be enough.