How to show/hide panel param?

Hi,
I’m trying to show/hide some param in my panel
Starting from the panel example, I’ve tried to “change” param.Integer behavior using pn.widgets.IntSlider (which supports show/hide by using visible right?):

import param
class RoomOccupancy(param.Parameterized):
    variable  = param.Selector(objects=list(data.columns))
    window    = param.Integer(default=10, bounds=(1, 20))
    sigma     = param.Number(default=10, bounds=(0, 20))
    button    = param.Boolean(bounds=(0, 1), default=False)

    @pn.depends('button', watch=True)
    def show_widget(self):
        print("show_widget:", self.button)
        pn.widgets.IntSlider.from_param(self.param.window, visible=self.button)

    def view(self):
        self.show_widget()
        return find_outliers(self.variable, self.window, self.sigma, hvplot2)

obj = RoomOccupancy()
occupancy = pn.Column(
    pn.Row("## Room Occupancy\nHover over the plot for more information.", obj.param),
    pn.Row(obj.view),
    pn.Row(table))
occupancy.show()

But I cannot get window to show/hide…
And I don’t really understand how to actually use my custom IntSlider in place of param.Integer…
Any help or explantion would be great
Best

I haven’t run your code but the show_widget method seems not to do anything, it’s neither setting an attribute nor returning something.

You can see in this repo an example of a class used to display basic and advanced configuration widgets. By default only the basic parameters are displayed, a check box allows to also display the advanced ones. This takes advantage of the precedence parameter a Parameter can have.

Ok thanks for you help.
Are there other way to proceed?

I remember reading about a visible argument here
Is there such a visible argument for param objects?

There are really two ways:

  1. Either you embrace the Param way and you use the fact that Panel understands the precedence parameter of a Parameter (e.g. x = param.Number(precedence=-1) when it displays the Parameters of a Parameterized class as widgets. If your example when you display obj.param with Panel, Panel will display all the widgets since the Parameters they’ve been created from all have a default positive precedence.
  2. Or you embrace the Panel way and then you will have to create the widgets by yourself (for instance as you did with the call of from.param), lay them out and display them, and implement the logic to hide them by changing their visible attribute when a button is clicked.

Here are these two ways described with an example each:

import param
import panel as pn

pn.extension()

class Example1(param.Parameterized):
    variable  = param.Selector(objects=list('abcdef'), precedence=1)
    window    = param.Integer(default=10, bounds=(1, 20), precedence=1)
    sigma     = param.Number(default=10, bounds=(0, 20), precedence=1)
    hide_button    = param.Boolean(bounds=(0, 1), default=False)
    
    @param.depends('hide_button', watch=True)
    def _hide_them(self):
        precedence = -1 if self.hide_button else 1
        for p_name in ['variable', 'window', 'sigma']:
            self.param[p_name].precedence = precedence

ex1 = RoomOccupancy()

# Display this in a notebok
pn.panel(ex1)

class Example2(param.Parameterized):
    variable  = param.Selector(objects=list('abcdef'))
    window    = param.Integer(default=10, bounds=(1, 20))
    sigma     = param.Number(default=10, bounds=(0, 20))
    hide_button    = param.Boolean(bounds=(0, 1), default=False)
    
    def __init__(self, **params):
        super().__init__(**params)
        self.view = pn.Param(self.param)
    
    @param.depends('hide_button', watch=True)
    def _hide_them(self):
        visible = not self.hide_button
        for p_name in ['variable', 'window', 'sigma']:
            self.view._widgets[p_name].visible = visible

ex2 = Example2()

# Display this in a notebok
ex2.view
3 Likes