Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying to output widget from button click callback throws exception #134

Closed
kh90909 opened this issue Aug 5, 2015 · 5 comments · Fixed by #176
Closed

Displaying to output widget from button click callback throws exception #134

kh90909 opened this issue Aug 5, 2015 · 5 comments · Fixed by #176
Labels
bug resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Milestone

Comments

@kh90909
Copy link

kh90909 commented Aug 5, 2015

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.

@kh90909
Copy link
Author

kh90909 commented Aug 5, 2015

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.

@kh90909
Copy link
Author

kh90909 commented 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

@jdfreder jdfreder added this to the 4.1 milestone Aug 10, 2015
@jdfreder jdfreder added the bug label Aug 10, 2015
@jdfreder
Copy link
Contributor

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.

@jdfreder jdfreder self-assigned this Oct 14, 2015
@minrk
Copy link
Contributor

minrk commented Oct 15, 2015

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.

@jdfreder
Copy link
Contributor

Thanks for clarifying @minrk

@github-actions github-actions bot added the resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion. label Feb 14, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants