I want to press the “random” button and trigger view with a random value.
import param
import numpy as np
import panel as pn
import holoviews as hv
pn.extension()
class TestPanel(param.Parameterized):
value = param.Integer(default=3)
random = param.Action()
@param.depends('random')
def generate_random(self):
self.view(value=int(np.random.rand(1) * 10))
@param.depends('value')
def view(self, value=None):
if value is None:
value = self.value
return hv.Curve([0, 1, 2, value])
test_panel = TestPanel()
pn.Column(test_panel.view, test_panel.param)
Also is there a method to replace self.value’s value manually?
Essentially, I want the equivalent of this with a Param class:
Hi @Marc, This implementation works great, but recently it failed in combination with a pn.widgets.FileDownload !
Having two or more param.Action() buttons with your implementation in combination with a pn.widgets.FileDownload() and the notebook crashes when trying to display the filedownload widget. Weird, I know. See first gif.
I think you are experiencing the infinite Parameterized.__repr__ recursion issue.
When you have more than one action on a param.Parameterized object an infinite recursion can be triggered. This is caused by the __repr__ function. The implication of this is that your app will crash without any useful information. And you waste a a lot of time figuring this out. Or you just leave Panel behind.
I’ve really tried to communicate that this is a fundamental problem for param and Panel. But so far without luck. See https://github.com/holoviz/param/issues/396. Please upvote if you think this is important to get solved.
You can solve the infinite recursion problem by overriding the __repr__ method.
Ok, glad to hear it has occured before, because I did struggle to understand it. Not fully clear why my alternative instantiation of the param.Action() is not triggering infinite recursion and crashing the notebook.
I now discovered other notebook crashes (which annoyed me before) are also related to multiple param.Action objects and appear when I return a parameterized object anywhere in the notebook.see gif. However the same workaround with direct instantiation with callback resolves it.