holoviz / param

Param: Make your Python code clearer and more reliable by declaring Parameters
https://param.holoviz.org
BSD 3-Clause "New" or "Revised" License
434 stars 74 forks source link

Add some pattern to allow preprocessing parameter value on change #332

Open philippjfr opened 5 years ago

philippjfr commented 5 years ago

In some cases you want to be able to define a @param.depends decorator which watches a parameter and then modifies it. This currently leads to recursion, it may therefore be nice if we had a recursion flag, e.g. to allow doing things like:

@param.depends('polys', watch=True, recursion=False)
def _init_polys(self):
    self.polys = self.polys.options(color='red')

Alternative suggestions to achieve the same thing would be welcome.

philippjfr commented 5 years ago

Actually this proposal is probably not sufficient for my purpose, what I really would need is a preempt keyword which basically says "execute me, cancel any existing events and process new events triggered by me but don't call me a second time". Fairly complex and maybe better expressed as a parameter preprocessor, something like:

@param.preprocess('polys')
def _preprocess_polys(self, polys):
    return self.polys.options(color='red')
jbednar commented 5 years ago

How about a context manager?

@param.depends('polys', watch=True)
def _init_polys(self):
    with param.watching_disabled():
        self.polys = self.polys.options(color='red')