How do I use param.Action in panel?

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

class TestPanel(param.Parameterized):
    value = param.Integer(default=3)
    random = param.Action()

    def generate_random(self):
        self.view(value=int(np.random.rand(1) * 10))
    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:

def plot(value):
    holoviews.object = hv.Curve([0, 1, 2, value])

def update_random(event):
    spinner.value = int(np.random.rand(1) * 10)

def update_spinner(event):
    return plot(

holoviews = pn.pane.HoloViews(hv.Curve([0, 1, 2, 3]))

spinner = pn.widgets.Spinner(name='Value', value=3), 'value')

button = pn.widgets.Button(name='Random'), 'clicks')

pn.Column(holoviews, spinner, button)


Hi @ahuang11

Something like the below should do the trick

import holoviews as hv
import numpy as np
import panel as pn
import param

DEFAULT_PLAT = hv.Curve([0,1,2,3])

class App(param.Parameterized):
    value = param.Integer(default=3)
    set_random_value = param.Action(label="Random")

    settings_pane = param.Parameter()
    plot_pane = param.Parameter()
    view = param.Parameter()

    def __init__(self, **params):

        self.set_random_value = self._set_random_value
        self.settings_pane = pn.Param(self, parameters=["value", "set_random_value"])
        self.plot_pane = pn.pane.HoloViews(DEFAULT_PLAT)
        self.view = pn.Column(self.plot_pane, self.settings_pane)

    @param.depends("value", watch=True)
    def _update_plot_pane(self):
        self.plot_pane.object = hv.Curve([0,1,2,self.value])

    def _set_random_value(self, event):
        self.value = int(np.random.rand(1) * 10)

1 Like