jupyter / help

:sparkles: Need some help or have some questions? Please visit our Discourse page.
https://discourse.jupyter.org
291 stars 97 forks source link

Jupyter notebook widgets.Button do not show up when putting in a try and except block #435

Closed masaguaro closed 5 years ago

masaguaro commented 5 years ago

I am working on Windows and Jupyter notebook. I was trying to execute a try and except block, in a Jupyter cell, with some widgets.Button within but it does not work. I have simplified the situation in the following code:

import ipywidgets as widgets

def widget(description):
    button = widgets.Button(description=description,layout={'width': '300px'})
    return button

try:
    print("Before widget_1")
    widget_1 = widget(description='Browser One')
    widget_1
    print("After widget_1")
except:
    pass

widget_2 = widget(description='Browser Two')
widget_2

After executing the Jupyter cell it will print " Before widget_1"and "After widget_1", and then widget_2will show up.

Browser Two

However, widget_1 does not show up.

Any idea why it does not work as I expected ?

minrk commented 5 years ago

widgets only show up on the page when they are displayed. If you want to display a widget:

from IPython.display import display
display(widget)

widget_2 is being displayed there because it is the last statement in the cell, which IPython interprets as the 'result' of the cell, so it gets displayed automatically.

masaguaro commented 5 years ago

@minrk Thank you very much!