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.

Displaying to output widget from button click callback throws exception

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kh90909commented, Aug 5, 2015

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)

image

0reactions
jdfredercommented, Oct 15, 2015

Thanks for clarifying @minrk

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output widgets: leveraging Jupyter's display system
The Output widget can capture and display stdout, stderr and rich output ... Button( description='click me to raise an exception', layout={'width': '300px'} ) ......
Read more >
Python Panel Button Callback Function - Stack Overflow
I have two functions which I want to be executed when a respective button is clicked. The first one is a function to...
Read more >
Motif Programming Manual (Volume 6A): 27 - IST
The callback ** for this button displays a new dialog that gives help. ... If the user does not click on a valid...
Read more >
Button | Android Developers
A user interface element the user can tap or click to perform an action. To display a button in an activity, add a...
Read more >
Error Boundaries - React
Event handlers (learn more); Asynchronous code (e.g. setTimeout or requestAnimationFrame callbacks); Server side rendering; Errors thrown in the error boundary ...
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