Disable param widgets

Is it possible to define parameters, for which the panel rendering is greyed out?
E.g. a param.Integer which panel renders a slider widget for should be updateble programmatically by a dependency/watcher (and update it’s rendering) but should not be usable/slidable for the user.

Can you try this out and see if it meets with your needs?

import panel as pn
import param

pn.extension()

class P(param.Parameterized):
    i = param.Integer(0, bounds=(0, 10), step=1, constant=True)
    incr = param.Event(label='Increment i')
    
    @param.depends('incr', watch=True)
    def _increment_i(self):
        with param.edit_constant(self):
            self.i += self.param.i.step


pn.panel(P()).servable()

The idea is to set constant=True on the Integer Parameter, which will be displayed as a disabled widget, but update it anyway with param.edit_constant.

Or, and that’s maybe a less hacky approach, you can let Panel know that the widget created for the Integer Parameter should be disabled by default, and update it without having to fiddle with constant and param.edit_constant:

import panel as pn
import param

pn.extension()

class P(param.Parameterized):
    i = param.Integer(0, bounds=(0, 10), step=1)
    incr = param.Event(label='Increment i')
    
    @param.depends('incr', watch=True)
    def _increment_i(self):
        self.i += self.param.i.step


pn.Param(P(), widgets={'i': {'disabled': True}}).servable()
1 Like

Great, thank you @maximlt for your super fast reply, both approaches fit my needs.

Only little drawback for both solutions:
While the parameter value updates as expected, the corresponding widget does not update.
E.g. a param.Integer slider is in start/default position and does not move.

Not a real problem, I just wanted to mention that behaviour.

I do see the widget value changing when I hit the button.

Kapture 2023-06-22 at 23.00.49

There has been issues recently with a bug in JupyterLab 4, that was worked around in the latest version of Panel released yesterday. So I’d suggest you try:

  • to serve the notebook/script with panel serve file --show to check whether it works as expected
  • upgrade Panel, or alternatively downgrade JupyterLab to <4 (the former should be easier)
1 Like