Skip to content

Instantly share code, notes, and snippets.

@LinuxIsCool
Created March 24, 2024 00:39
Show Gist options
  • Save LinuxIsCool/044bda6eb4efa7065321ee102e2b5bfa to your computer and use it in GitHub Desktop.
Save LinuxIsCool/044bda6eb4efa7065321ee102e2b5bfa to your computer and use it in GitHub Desktop.
Python Param Panel with Resettable Button
import param as pm
import panel as pn
import hvplot.pandas
import pandas as pd
import numpy as np
pn.extension()
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(pn.Param(self.param), reset_button)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment