Skip to content

Instantly share code, notes, and snippets.

@bhargavkakadiya
Last active November 23, 2023 06:59
Show Gist options
  • Save bhargavkakadiya/b890b92837c1738c1f33bd058a196570 to your computer and use it in GitHub Desktop.
Save bhargavkakadiya/b890b92837c1738c1f33bd058a196570 to your computer and use it in GitHub Desktop.
Simple example for Param Panel class interactions
## Run it on jupyter notebook for interactive panel
import param as pm
import panel as pn
pn.extension()
class A(pm.Parameterized):
a = pm.Number(2, step=1)
def get_a_squared(self):
return self.a * self.a
class B(pm.Parameterized):
def __init__(self, a:A, **params):
super().__init__(**params)
self.a = a
b = pm.Number(2, step=1)
@pm.depends('a.a', 'b') # a.param also works
def sum(self):
return self.a.a + self.b
@pm.depends('a.get_a_squared', 'b') # a.param also works
def sum_of_squares(self):
return self.a.get_a_squared() + (self.b*self.b)
a = A()
b = B(a)
pn.Row(
pn.Column(a, b),
pn.Column('Sum: ', b.sum, 'Sum of Squares:', b.sum_of_squares)
)
## Result
## changing parameter a and b should reflect change in sum and its squares
@LinuxIsCool
Copy link

Good base case to remember. Thanks @bhargavkakadiya

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment