Ipywidgets: Play widget won't call the function

Created on 24 Oct 2017  路  8Comments  路  Source: jupyter-widgets/ipywidgets

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?

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:

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])

All 8 comments

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:

  1. We are using jslink, so the change is being directly communicated between models on the javascript side.
  2. When the play button increments, the jslink model sees it and updates the slider value.
  3. The slider value is automatically synced to the backend. Now, there is a question about where output should go from that update. Usually when a slider is directly updated, we kludge a shortcut for convenience to associate any output with the output region the widget lives in. However, in this case the slider was updated from who-knows-where (as far as it is concerned), so it is not clear where to send output.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

npetitclerc picture npetitclerc  路  4Comments

hangyao picture hangyao  路  6Comments

pleabargain picture pleabargain  路  5Comments

kebwi picture kebwi  路  6Comments

languy picture languy  路  3Comments