Here's a minimal example:
%matplotlib inline
from IPython.html import widgets
from IPython.display import display
def click(b):
with out:
print 'Hello world'
out=widgets.Output()
button=widgets.Button(description='Click Me')
button.on_click(click)
display(out)
display(button)
Executing a notebook cell with this code and clicking the button results in the following exception being thrown:
NameError: global name 'get_ipython' is not defined
This is with the Anaconda ipython / ipython-notebook version 3.2.1 packages on Windows.
This issue can be resolved by adding
from IPython import get_ipython
to IPython/html/widgets/widget_output.py, but I'm not familiar enough with the code to be sure if this is the correct place to make that change, or if the fix should be further upstream.
The minimal test case above raises another potential issue, but I haven't convinced myself that this is not just an error on my part:
When the button is clicked, only blank space is added to the output widget. 'Hello world' is not printed.
Also, here's a more functional example of updating an output widget from a button click callback, to show how this can be used to position output (e.g. a matplotlib plot) relative to other widgets:
%matplotlib inline
# To prevent automatic figure display when execution of the cell ends
%config InlineBackend.close_figures=False
import matplotlib.pyplot as plt
import numpy as np
from IPython.html import widgets
from IPython.display import display,clear_output
plt.ioff()
ax=plt.gca()
plt.plot(np.random.randn(100),np.random.randn(100),'+')
out=widgets.Output()
button=widgets.Button(description='Next')
vbox=widgets.VBox(children=(out,button))
display(vbox)
def click(b):
ax.lines[0].set_xdata(np.random.randn(100))
ax.lines[0].set_ydata(np.random.randn(100))
with out:
clear_output(wait=True)
display(ax.figure)
button.on_click(click)
click(None)

from IPython import get_ipython
to IPython/html/widgets/widget_output.py, but I'm not familiar enough with the code to be sure if this is the correct place to make that change, or if the fix should be further upstream.
That's right, get_ipython() used to be available globally, looks like that's no longer the case after the big split.
Thanks for the excellent example of how a plot can be relocated! Feel free to open a PR to add that to the examples folder.
The minimal test case above raises another potential issue, but I haven't convinced myself that this is not just an error on my part:
When the button is clicked, only blank space is added to the output widget. 'Hello world' is not printed.
I wasn't able to reproduce this, it may be fixed in master.
That's right, get_ipython() used to be available globally, looks like that's no longer the case after the big split.
Since the bug report is using IPython prior to the split, I don't think that's the case. The issue is that we inject get_ipython into globals only while interactive user code is running. Comm messages may not be handled in that context (arguable whether they should be). In general, though, no library code should rely on get_ipython in globals, and always import it. Only interactively typed code should access it via globals.
Thanks for clarifying @minrk
Most helpful comment
Also, here's a more functional example of updating an output widget from a button click callback, to show how this can be used to position output (e.g. a matplotlib plot) relative to other widgets: