You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# make sure prints in callbacks make it to the notebookdefsubplots(*args, **kwargs):
fromIPython.displayimportdisplayimportipywidgetsaswidgetsimportweakrefimportfunctoolsfig, ax=plt.subplots(*args, **kwargs)
fig._output=output=widgets.Output()
display(output)
orig_mpl_connect=fig.canvas.mpl_connect@functools.wraps(orig_mpl_connect)defmpl_connect(key, cb, **kwargs):
try:
r=weakref.WeakMethod(cb)
exceptTypeError:
r=lambda: cbdefwrapper(*args, **kw):
cb=r()
withoutput:
ifcbisNone:
...
else:
cb(*args, **kw)
orig_mpl_connect(key, wrapper, **kwargs)
fig.canvas.mpl_connect=mpl_connectreturnfig, ax
which relies on display being called in just the right order and monkey-patches the canvas, but I think something like this could be backed into ipympl?
'''make sure prints in callbacks make it to the notebookSee https://ipywidgets.readthedocs.io/en/stable/examples/Output%20Widget.html By default, calling `print` in a ipywidgets callback results in the output being lost (because it is not clear _where_ it should go). You can explictily capture that the text to a given output area using at Output widget.This is a wrapper for `plt.subplots` that makes sure a) an ipywidgets.widgets.Output is created with each Figure b) the `mpl_connect` on the canvas is monkey-patched such that all user callbacks run in a context where the stdout is captured and sentto that output area.'''importmatplotlib.pyplotaspltdef_monkey_patch_pyplot():
importmatplotlib.pyplotaspltimportfunctoolsfromIPython.displayimportdisplayimportipywidgetsaswidgetsimportweakref@functools.wraps(plt.figure)deffigure(*args, **kwargs):
fig=figure._figure(*args, **kwargs)
fig._output=output=widgets.Output()
display(output)
orig_mpl_connect=fig.canvas.mpl_connect@functools.wraps(orig_mpl_connect)defmpl_connect(key, cb, **kwargs):
# try to use a WeakMethod to make sure we don't keep objects alive# to match the behavior of the base mpl_connecttry:
r=weakref.WeakMethod(cb)
exceptTypeError:
defr():
returncbdefwrapper(*args, **kw):
cb=r()
withoutput:
ifcbisnotNone:
cb(*args, **kw)
orig_mpl_connect(key, wrapper, **kwargs)
# mokeny patch the canvasfig.canvas.mpl_connect=mpl_connectreturnfig# stash the orginalfigure._figure=plt.figure# monkey patch pyplot (!?)plt.figure=figureplt._print_hacked=True# make sure we only do this once!ifgetattr(plt, '_print_hacked', False):
...
else:
_monkey_patch_pyplot()
# clean up after our selves and do not polute the user's namespacedel_monkey_patch_pyplotdelplt
We're running into similar issues and I'm playing with the suggestion above. An interesting case that I run into, that may be of interest: if an Output widgets is used (as a context manager) in a fresh asyncio task (i.e. not during a COM event) both prints and errors are swallowed (i.e. not shown anywhere).
But this does not. It shows "hello 1" in the global log widget. But "hello 2" is never shown.
It would be interesting to see if "hello 2" gets transmitted to the frontend over the websocket connection. If it does, there's probably a change in jupyterlab we could make to get it into the log.
In general, running code outside of the framework of the kernel execute or comm messages (like in this async case) messes with the output capturing and redirection that the ipython kernel does.
Activity
SylvainCorlay commentedon Jul 4, 2019
This is (unfortunately) the expected behavior in JupyterLab.
JupyterLab does not show outputs from callbacks triggered by a widget change. The use of the output widget is the way to go.
SylvainCorlay commentedon Jul 4, 2019
Note: there is ongoing work to add a "global output area" for display events that are not tied to a specific cell, which may be useful for debugging.
tacaswell commentedon Jul 4, 2019
A very hacky way to make this work is:
which relies on
displaybeing called in just the right order and monkey-patches the canvas, but I think something like this could be backed into ipympl?tacaswell commentedon Jul 7, 2019
A slightly more thought out version:
almarklein commentedon Aug 18, 2021
We're running into similar issues and I'm playing with the suggestion above. An interesting case that I run into, that may be of interest: if an
Outputwidgets is used (as a context manager) in a fresh asyncio task (i.e. not during a COM event) both prints and errors are swallowed (i.e. not shown anywhere).ianhi commentedon Aug 19, 2021
@almarklein do you have a short example you could post?
almarklein commentedon Aug 19, 2021
Yes. So this works fine, printing "hello 2" in the cell output.
But this does not. It shows "hello 1" in the global log widget. But "hello 2" is never shown.
A screenshot of the result:

jasongrout commentedon Aug 20, 2021
It would be interesting to see if "hello 2" gets transmitted to the frontend over the websocket connection. If it does, there's probably a change in jupyterlab we could make to get it into the log.
In general, running code outside of the framework of the kernel execute or comm messages (like in this async case) messes with the output capturing and redirection that the ipython kernel does.