Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

observe method of ipywidget slider doesn't work

I am trying to call function on slider change, while it is changing with animation. And it does not work, it calles function only when I change manually by mouse. Here is my code:

import ipywidgets as widgets
    
play = widgets.Play(
    value=0,
    min=0,
    max=100,
    step=1,
    description="Press play",
    disabled=False)

slider = widgets.IntSlider(description="Slides")
   
def on_value_change(change):
    print(change['new'])
        
slider.observe(on_value_change, names='value') 
widgets.jslink((play, 'value'), (slider, 'value'))
widgets.HBox([play, slider])

Could you help me to manage it, please?

like image 285
Sergey Vladimirovich Avatar asked Sep 04 '25 17:09

Sergey Vladimirovich


1 Answers

From this issue (https://github.com/jupyter-widgets/ipywidgets/issues/1775) it looks like the output from the function needs be captured in an output widget. See Jason Grout's example:

import ipywidgets as widgets

out = widgets.Output()
def on_value_change(change):
    with out:
        print(change['new'])

slider = widgets.IntSlider(min=1, max=100, step=1, continuous_update=True)
play = widgets.Play(min=1, interval=2000)

slider.observe(on_value_change, 'value')
widgets.jslink((play, 'value'), (slider, 'value'))
widgets.VBox([play, slider, out])
like image 99
ac24 Avatar answered Sep 06 '25 19:09

ac24