Ipywidgets: print statement of ipywidgets not work in JupyterLab

Created on 10 May 2018  路  8Comments  路  Source: jupyter-widgets/ipywidgets

Phenomenon:

  • print statement not work in JupyterLab(can not see any output), but work in Jupyter Notebook

Environment:

  • Ubuntu 16.04
  • Chrome v66
  • JupyterLab 0.32.1
  • Python 3.6.5

Code:

from IPython.display import display
from ipywidgets import widgets
button = widgets.Button(description="Click Me!")
display(button)

def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)
resolved-locked

Most helpful comment

You'll need to use the output capturing mechanism in ipywidgets:

from IPython.display import display
from ipywidgets import widgets
button = widgets.Button(description="Click Me!")
display(button)

output = widgets.Output()

@output.capture()
def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)
display(output)

All 8 comments

I'm also running into the same problem, similar environment.

You'll need to use the output capturing mechanism in ipywidgets:

from IPython.display import display
from ipywidgets import widgets
button = widgets.Button(description="Click Me!")
display(button)

output = widgets.Output()

@output.capture()
def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)
display(output)

Another advantage of this method is that it lets you put that output where you want, not just in that cell.

I remember there is jupyter document describing the necessity for such breaking change, but google doesn't help me...
Since this is a landing-issue, @jasongrout can you provide some architectural link on what is going?

Closing as answered.

@jasongrout, can @output.capture be used inside a class? And how or where do you place this?

@jasongrout, can @output.capture be used inside a class? And how or where do you place this?

Stumbled across this. Here is a naive way...

```import ipywidgets as widgets
from IPython.display import display

class ExampleButton(widgets.Button):
output = widgets.Output()

@output.capture()
def on_button_clicked(b):
    print("Button clicked.")

button = ExampleButton(description="Click Me!")
display(button)

button.on_click(ExampleButton.on_button_clicked)
display(ExampleButton.output)

Was this page helpful?
0 / 5 - 0 ratings