ipykernel
ipykernel copied to clipboard
GUI integration in Jupyter notebook with do_one_iteration does not work
Kernel: Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] IPython version: 7.9.0 Jupyter notebook and Jupyter lab show the same behaviour.
I am trying to set up a GUI integration as given in the documentation for notebooks. Here is a minimal example:
@register_integration('minimalexample')
def loop_tk(kernel):
"""Start a kernel with the Tk event loop."""
from tkinter import Tk
# Tk uses milliseconds
poll_interval = int(1000*kernel._poll_interval)
# For Tkinter, we create a Tk object and call its withdraw method.
class Timer(object):
def __init__(self, func):
self.app = Tk()
self.app.withdraw()
self.func = func
def on_timer(self):
self.func()
self.app.after(poll_interval, self.on_timer)
def start(self):
self.on_timer() # Call it once to get things going.
self.app.mainloop()
kernel.timer = Timer(kernel.do_one_iteration)
kernel.timer.start()
However, the following behaviour occurs:
After %gui minimalexample
, the first cell execution request is not executed (it shows a star in the cell status). Every consecutive request works, until a request that triggers an error is executed. Every cell execution request that happens afterwards is not successful, the star is shortly shown, no output. I see also that the event loop integration in the source code is solved differently now, by executing the event loop that should be integrated and falling back to the original event loop on zmq socket activity, but I haven't found a possibility in my case to exit the event loop without tearing down the complete GUI (I am integrating WinForms via PythonNet). Is the way of integration as shown in the documentation deprecated? If not, I would say this is a bug, if yes, the documentation should be updated.
I am not sure if this is related but I faced a similar problem while using Ipyvolume which uses a similar setup to take a screenshot. Kindly see: https://github.com/maartenbreddels/ipyvolume/blob/285d0b187f659ac2948523520414a26dd2701f5b/ipyvolume/pylab.py#L1210 Is there a workaround for the above issue?