4

How can a function watch for value changes of a panel.widgets.IntSlider, but only trigger when the mouse is released rather than continuously as the slider is dragged around?

I have tried callback_policy='mouseup' and decorated the function with @panel.depends(panel.widgets.IntSlider, watch=True), but the function is executed continuously when the slider is pulled around an not only when the mouse button is released. This is the full code:

import panel as pn


pn.extension()
int_slider = pn.widgets.IntSlider(
    name='Integer Slider', end=5, callback_policy='mouseup')

@pn.depends(int_slider.param.value, watch=True)
def print_slider_value(slider_value):
    return slider_value

pn.Column(int_slider, print_slider_value) #.app('localhost:8888')

I have tried both with and without .app() in the end and with 'throttle' instead of 'mouseup', same result. I am trying this in the JupyterLab notebook and I have the PyViz extension installed.

bokeh       1.2.0
panel       0.6.0
IPython     6.5.0
jupyter_client  5.2.3
jupyter_core    4.4.0
jupyterlab  1.0.2
notebook    5.6.0
joelostblom
  • 43,590
  • 17
  • 150
  • 159

1 Answers1

2

I checked the source code of panel and found that it didn't define the value_throttled property. To solve the problem, you need to create a class that inheritance from IntSlider and add the value_throttled property. Then you can watch on value_throttled.

import panel as pn
import param

class IntThrottledSlider(pn.widgets.IntSlider):
    value_throttled = param.Integer(default=0)

int_slider = IntThrottledSlider(
    name='Integer Slider', end=5, callback_policy='mouseup')

@pn.depends(int_slider.param.value_throttled, watch=True)
def print_slider_value(slider_value):
    return slider_value

pn.Column(int_slider, print_slider_value)
HYRY
  • 94,853
  • 25
  • 187
  • 187
  • What does the equivalent usage of `@param.depends` look like if you want a method of a `param.Parameterized` object to watch the throttled value of a parameter? I would expect something like `param.depends('x.value_throttled')` to work, but no luck. Hard to search for this question, too. – Tim Morton May 18 '21 at 22:38