I am using Jupyter notebook and trying to make its Play widget work. As a start, I want to create a slider that would automatically roll over its range and execute the connected function. Consider the following code:
import ipywidgets as widgets
def on_value_change(change):
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])
As expected, whenever I click back and forth on the slider, the function on_value_change is called and the current value is printed on the output. However, when I launch the animation via the play button, nothing is printed. In fact, it looks like on_value_change is not even called. Could anyone please explain the reason? Is it a bug?
This has to do with telling the system where output should go. For example, this works because we explicitly capture the output into an output widget:
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])
And technically, I think what is happening is this:
Point 3 may be clearer if you have the display the play widget in one cell, and slider twice in two different cells. Where should output go? In the output associated with the play widget, where the user actually interacted with a control? Or in the first or second displayed slider cells, where the slider is actually moving?
import ipywidgets as widgets
def on_value_change(change):
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'))
play
md5-1ba9ce47f18883b1eb241b101e59b902
```python
slider
That's why explicitly capturing the output and displaying it where ever you want works better in general.
@jasongrout Thanks for the response. However, your solution doesn't seem to work either... I'm using Jupyter 4.2.0 with Python 2.7.12 and Chrom 61.0.3163.100 on Ubuntu 16.04
What version of ipywidgets, widgetsnbextension, and notebook do you have?
ipywidgets==5.2.2
widgetsnbextension==1.2.6
notebook==4.2.3
Please see below my full pip freeze:
freeze.txt
Ah. Upgrading to ipywidgets 7 will give you that Output widget.
Upgrading, then jupyter nbextension enable --py widgetsnbextension helped! Thanks!
Great! I'll close this issue as answered, but feel free to ask questions here, or on gitter, or in a new issue.
Most helpful comment
This has to do with telling the system where output should go. For example, this works because we explicitly capture the output into an output widget: