display `print` in callbacks in the notebook
See original GitHub issueimport matplotlib.pyplot as plt
fig, ax = plt.subplots()
n = 0
def printer(event):
global n
print(f'in event {event}')
n += 1
fig.canvas.mpl_connect('button_press_event', printer)
when run in IPython prints out to stdout on every mouse click. In jlab + ipympl the prints don’t show up anywhere I can find…
From https://ipywidgets.readthedocs.io/en/stable/examples/Output Widget.html it looks like this can work via:
output = widgets.Output()
display(fig.canvas, output)
n = 0
def printer(event):
global n
with output:
print(f'in event {event}')
n += 1
fig.canvas.mpl_connect('button_press_event', printer)
but I suspect there is a way for this to work automatically…
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
callback function doesn't print, unless ran by Jupyter
I call an API with a callback function that prints what comes out of the API call. If I run this code in...
Read more >Widget Events — Jupyter Widgets 7.6.2 documentation
The callback will be called with one argument, the clicked button widget ... To capture print s (or any other kind of output)...
Read more >Part 3. Basic Callbacks | Dash for Python Documentation | Plotly
Graph component to display new data. We could also update the style of a component or even the available options of a dcc.Dropdown...
Read more >Writing your own callbacks | TensorFlow Core
Callbacks are useful to get a view on internal states and ... print("Start epoch {} of training; got log keys: {}".format(epoch, keys))
Read more >Using with Jupyter — Bokeh 2.4.1 Documentation
To display Bokeh plots inline in a classic Jupyter notebook, ... connecting plot events and Bokeh's built-in widgets directly to Python callback code....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

A slightly more thought out version:
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.