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.

Callbacks not triggered when using observe

See original GitHub issue

Hi I have the following problem.

When I use:

import ipywidgets as widgets

toggle = widgets.ToggleButton(description='click me')

def on_click(change):
    print(change['new'])

toggle.observe(on_click, 'value')

the callback does not get triggered, but if I use

import ipywidgets as widgets

@interact
def clicked(a=widgets.ToggleButton()):
    print(a)

the call back gets triggered.

I am using Jupyter Lab: Jupyter Notebook version: 5.6.0 ipywidgets version: 7.2.1

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
jasongroutcommented, Jul 25, 2018

You’ll need to use an output widget to capture the printed output: http://ipywidgets.readthedocs.io/en/stable/examples/Output Widget.html.

out = Output()
display(out)

@out.capture()
def on_click(change):
    print(change['new'])

1reaction
DavidJVitalecommented, Nov 15, 2018

@vidartf Thanks for your help. That code snippet functions the same in the live notebook on Classic server and JupyterLab: The problem is that it still stores all those <h3> HTML elements inside of a dynamic JS widget. If you run your code and export to HTML, you need JS enabled (and some libraries loaded from CDN) to view the Arbitrary HTML text. Try running your cell, exporting to HTML, and doing an ‘Inspect Element’. You’ll just see <script> tags in the output, and no text.

Luckily, I have a found a solution for my problem, using the display_handler = IPython.display.display("foo", display_id="id") and display_handler.update() functionality. Below is the code snippet that works in both JupyterLab and Classic Notebook Server. If you export after running it, you will see the raw <h3> element in the output cell.

from ipywidgets import *
from IPython.display import display, HTML
from uuid import uuid4
w = IntSlider()
pure_html_display_hander = display(HTML('starting text'),
                                   display_id = str(uuid4()))

out = Output()
display(out)
display(w)

@out.capture()
def callback(change):
    display_html = HTML(f'<h3>Arbitrary HTML {uuid4()}</h3>')
    pure_html_display_hander.update(display_html)

w.observe(callback, names=['value'])

It’s a bit hacky updating a global from inside a callback, but it fits my need. I am not sure if anyone else has ran into this issue, but this is the best way to dynamically update output cell HTML of the underlying notebook file asyncronously in both JupyterLab and Classic Notebook Server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Observe callback is not being triggered with Kotlin coroutines ...
I'm new to Android development and trying to understand Coroutines and LiveData from various example projects. I have currently setup a function ...
Read more >
ractive.observe() | Docs
Observes the data at a particular keypath. Unless specified otherwise, the callback will be fired immediately, with undefined as oldValue .
Read more >
IntersectionObserver.observe() - Web APIs - MDN Web Docs
The IntersectionObserver method observe() adds an element to the set of target elements being watched by the IntersectionObserver .
Read more >
5 challenges using Plotly Dash for web apps | Analytics Vidhya
For instance, if the Div defined above does not have explicitly the style property referenced in the Python callback, the Javascript code will...
Read more >
Solved: Not getting observeField (or observeFieldScoped) t...
Not getting observeField (or observeFieldScoped) to firing callback ... Trying to mash together two (or three) examples I found, to create a ...
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