I am trying to create something like a parameter property
on Parameterized
class. Because I would like be able to display and layout the properties
in Panel via the pn.Param
parameter.
As far as I can see this is not supported “out of the box” (?) so I’m creating the below workaround
import param
class SpreadFeatures(param.Parameterized):
"""A class containing features of a spread"""
mean_10d_5d_rolling = param.Number()
std_10d_5d_rolling = param.Number()
mean_to_std_10d_5d_rolling = param.Number(None, constant=True)
@param.depends("mean_10d_5d_rolling", "std_10d_5d_rolling", watch=True)
def set_mean_to_std_10d_5d_rolling(self):
print("update")
with param.edit_constant(self):
if self.std_10d_5d_rolling:
self.mean_to_std_10d_5d_rolling = self.mean_10d_5d_rolling / self.std_10d_5d_rolling
else:
self.mean_to_std_10d_5d_rolling = None
features = SpreadFeatures(mean_10d_5d_rolling=1, std_10d_5d_rolling=2)
When I use IPython I see
In [1]: %run scripts\issues\issue_param_constructor.py
In [2]: features
Out[2]: SpreadFeatures(mean_10d_5d_rolling=1, mean_to_std_10d_5d_rolling=None, name='SpreadFeatures00002', std_10d_5d_rolling=2)
In [3]: features.set_mean_to_std_10d_5d_rolling()
update
In [4]: features
Out[4]: SpreadFeatures(mean_10d_5d_rolling=1, mean_to_std_10d_5d_rolling=0.5, name='SpreadFeatures00002', std_10d_5d_rolling=2)
In [5]: features.std_10d_5d_rolling=3
update
In [6]: features
Out[6]: SpreadFeatures(mean_10d_5d_rolling=1, mean_to_std_10d_5d_rolling=0.3333333333333333, name='SpreadFeatures00002', std_10d_5d_rolling=3)
In [7]: features.std_10d_5d_rolling=3
update
In [8]: features
Out[8]: SpreadFeatures(mean_10d_5d_rolling=1, mean_to_std_10d_5d_rolling=0.3333333333333333, name='SpreadFeatures00002', std_10d_5d_rolling=3)
In [9]: import panel as pn
In [10]: pn.Param(features).show()
WARNING:tornado.access:404 GET /favicon.ico (::1) 0.99ms
I.e. the “setters” are not run when I construct the object. But only when I use the object after construction.
Is there a “param” way of specifying that events should be triggered during construction or do I need to override the parent constructor and implement this my self?
Is there a better way of achieving what I wan’t to do here?