Print output going to log window instead of cell in Jupyter Lab

Viewed 174

I recently switched from Jupyter Dashboard to Jupyter Lab and have some issues with widgets.

My notebook contains buttons which, when clicked, need to print text to the output cell below the code cell. They worked fine in Jupyter Dashboard. But in Jupyter Lab, the same text gets send to the log window. Any idea how to fix this ?

NOTE: When the print() function is not called from a button, the text is displayed below the code cell in both Jupyter Dashboard and Jupyter Lab.

enter image description here

1 Answers

Since button clicks are stateless, they are transmitted from the front-end to the back-end using custom messages. By using the on_click method, a button that prints a message when it has been clicked is shown below. To capture prints (or any other kind of output) and ensure it is displayed, be sure to send it to an Output widget (or put the information you want to display into an HTML widget).

button = widgets.Button(description="Click Me!")
output = widgets.Output()

display(button, output)

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

button.on_click(on_button_clicked)

https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Events.html

Related