Hi
I have a parameterized class with multiple parameters and one function that depends on a lot of these.
Sometimes I would like update multiple of these due to a change of “theme”. The problem is I have a function when depends on each of these parameters. And I only want it to run once for efficiency reasons.
What is the best way to achieve that?
Can I deactive the watchers while updating and then manually run the function when done? How?
Example
In the example below the dependent function is the _inc
function. I want to be able to update both param1
and param2
.
The actions in “red” are not working for me as they _inc
twice.
The action in “green” is working for me as it runs _inc
once.
But is it best practice?
import param
import panel as pn
class MultiParamApp(param.Parameterized):
count = param.Integer(0)
param1 = param.Integer(0)
param2= param.Integer(0)
update = param.Action()
update_once = param.Action()
update_once_disable_watchers = param.Action()
reset = param.Action()
view = param.Parameter()
def __init__(self, **params):
super().__init__(**params)
self._updating = False
self.update = self._update
self.update_once = self._update_once
self.update_once_disable_watchers = self._update_once_disable_watchers
self.reset = self._reset
self.view = pn.Param(self,
widgets = {
"update": {"button_type": "danger"},
"update_once": {"button_type": "danger"},
"update_once_disable_watchers": {"button_type": "success"},
}
)
@param.depends("param1", "param2", watch=True)
def _inc(self):
if not self._updating:
self.count += 1
def _update(self, _=None):
self.param1 = self.param1 +1
self.param2 = self.param2 +1
def _update_once(self, _=None):
self.param.set_param(param1= self.param1+1, param2= self.param2+1)
def _update_once_disable_watchers(self, _=None):
self._updating = True
self._update_once()
self._updating = False
self._inc()
def _reset(self, _=None):
self.count = 0
MultiParamApp().view.servable()