question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Play widget won't call the function

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jasongroutcommented, Oct 24, 2017

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])
1reaction
jasongroutcommented, Oct 24, 2017

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
slider
slider

That’s why explicitly capturing the output and displaying it where ever you want works better in general.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't call character functions in widget - Unreal Engine Forums
Hello, I'm creating a widget that calls a function from the character. It should display another widget with custom properties.
Read more >
Flutter: Call a function on a child widget's state - Stack Overflow
When I press the play button, my main class needs to call play() on the state's VideoPlayerController, so I created a function inside...
Read more >
Can't call a function from a widget. : r/unrealengine - Reddit
Hello, I am having problems calling a function from a widget (Have been following this series https://www.youtube.com/watch?v=QGIie_bDmeQ&list= ...
Read more >
Call function from another widget in [FLUTTER] - YouTube
MEDIUM:https://medium.com/@thefujii/how-to- call -a- function -from-another- widget -flutter-tutorial-283dfc431554JOIN OUR ...
Read more >
Music widget permanently on lock screen? - Apple Developer
Can't seem to figure out how to get rid of the music player on the lock screen ... call by replying to what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found