I have a class that uses IPython widgets to render rich output. This output changes over time. I manage this by adding a callback to the IOLoop that alters the output periodically.
class Foo(object):
def _ipython_display_(self, **kwargs):
html = HTML("<h2>hello</h2>")
def f():
html.value = ... # TODO: replace with weakrefs to avoid cycle
from tornado.ioloop import PeriodicCallback
html._pc = PeriodicCallback(f, 1000)
html._pc.start()
return html._ipython_display_(**kwargs)
However, I also want this PeriodicCallback to stop once html
is no longer visible on the screen. Normally I would use __del__
or finalize
or something, but it appears that the html
object will likely be kept in the notebook's output history (variables like _10
) and so my periodic callback survives forever.
Is there any way to get a signal when a displayable element is no longer displayed?