Last active
November 23, 2023 06:59
-
-
Save bhargavkakadiya/b890b92837c1738c1f33bd058a196570 to your computer and use it in GitHub Desktop.
Simple example for Param Panel class interactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good base case to remember. Thanks @bhargavkakadiya