-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Print events from matplotlib event handlers get swallowed #244
Comments
I think it's another case of "where should the output go". The kernel is probably sending the output, but the notebook frontend doesn't know which cell to associate it with, so it discards it. I seem to recall @jdfreder spent some effort working round this for widgets. It would be good to have a more general solution, but I'm not sure what shape that would take. |
Yeah, this is exactly the kind of thing I had to tackle with the widgets. The notebook isn't smart enough to know what cell triggered the click event, so you need to tell it before sending the message to the backend. Thanks for pinging me here @takluyver , this is one of the things I'm trying to reimagine with the output area work I'm doing. |
Threaded output is associated with the most recent execution (by the kernel). With a GUI backend, I see the output in the cell (whichever is the most recently executed). With the nbagg backend, the most recent request is likely to be a comm message, and the nbagg backend is presumably dropping those messages. The nbagg backend could hook up the cell output callback for output messages it receives for its comms. |
Mmmh... @ellisonbg, this is something that perhaps Ryan could work on as part of the MPL work.. . He might be able to make an improvement to the actual nbagg backend in MPL itself, working with @tacaswell (perhaps with some guidance from @minrk). What do you think? |
@blink1073 is also working on a widget based version of nbagg |
Yes, see #4582 for where I left off. matplotlib/matplotlib#4582 |
The issue I ran in to originally that the div created by |
When the following cell is executed, the %matplotlib notebook
import matplotlib.pyplot as plt
from IPython.html.widgets import interactive
fig, ax = plt.subplots()
ax.plot(range(5))
vline = ax.axvline(1, color='k')
hline = ax.axhline(0.5, color='k')
def set_cursor(x, y):
vline.set_xdata((x, x))
hline.set_ydata((y, y))
ax.figure.canvas.draw_idle()
interactive(set_cursor, x=ax.get_xlim(), y=ax.get_ylim()) |
@jdfreder, perhaps you have some insights that can help us here? |
@blink1073 if you want the div to be treated as something that can be used by interact, you'll need to write a custom widget view and python widget. The |
If instead you want something that doesn't display in the widget area, you may be able to fool it by having a Python class that inherits from HasTraits, having a traitlet called |
Not sure this should be closed. We also have an open mpl issue related to exceptions from event callbacks also being swallowed matplotlib/matplotlib#5111 |
OK, marking as 5.0 |
It could be argued that this should only be a bug on the mpl side, but I suspect that we will need some support from IPython on this one. |
Fixed by matplotlib/matplotlib#5754. |
Wonderful, thanks folks. Closing here (obviously users will need a newer mpl to get the fix, but that's fine). |
I would greatly appreciate a working example... in this code I can't even see the button
|
the original example still does not work, I don't see any prints under the plot did recent changes revert the fix? |
The changes referenced above were never released (it was targetted for 2.1) but in the mean time the widgets code has move through 3 major versions so the fixed nbagg did not work. The upcoming 2.1 release (the first rc will be announced as soon as the mac wheels finish building on travis!) will still use the old js. For a backend which is integrated with widgets see https://github.com/matplotlib/jupyter-matplotlib / ipympl |
I used the example posted by @hakim89:
It works as designed in my jupyter notebook: produces a big button, and whenever I click in the button, the word "click" is plotted on the button. However, I wanted to test out if the std out would print out in the cell, so I added Versions of relevant packages are included in the screenshot below (from Jupyter Notebook -> Help -> About). Also, matplotlib version is 2.2.2 |
Did you try restarting the kernel after switching to The no plot for ipympl should be a bug report against https://github.com/matplotlib/jupyter-matplotlib |
If the print statements are for debugging purposes, you can use import matplotlib.pyplot as plt
import os
fig, ax = plt.subplots()
ax.plot(np.random.rand(10))
def onclick(event):
msg = 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f\n'%(
event.button, event.x, event.y, event.xdata, event.ydata)
os.write(1, msg.encode())
cid = fig.canvas.mpl_connect('button_press_event', onclick) The messages will show up in the terminal that is running the jupyter notebook. |
This is still an issue for me. Using the example in the first message, I do not see any print statements anywhere in the notebook. I would like to see this issue re-opened. Versions (conda-forge):
Thanks |
Consider the following code in the notebook:
these events never print anywhere.
The code does run, as can be seen by this code from @tacaswell:
After clicking a couple of times:
Somehow we're swallowing that stdout completely, I'm not 100% sure why right now. But to the user, it feels very puzzling. Given the equivalent code works just fine at the terminal, we should try to make it work without jumping through additional hoops in the notebook.
The text was updated successfully, but these errors were encountered: