jupyter-widgets-contrib / ipycanvas

Interactive Canvas in Jupyter
https://ipycanvas.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
687 stars 64 forks source link

Question: how to tell if canvas is connected? #147

Closed dsblank closed 3 years ago

dsblank commented 3 years ago

This may be a question for ipylab but it may require some low-level canvas API:

I'm trying to make some start-up code that will add a canvas in a panel using ipylab:

app = JupyterFrontEnd()

panel = None
for widget in app.shell.widgets.values():
    if hasattr(widget, "title") and widget.title.label == "TITLE":
        panel = widget
        break

if panel is None:
    panel = Panel()
    panel.children = [canvas]
    panel.title.label = "TITLE" 
    app.shell.add(panel, 'main', {'mode': 'split-right'})
else:
    panel.children = [canvas]

where canvas is my ipycanvas canvas.

The problem is that in JupyterLab, the panel is in the app.shell.widgets, but it is no longer "connected" in HTML dom language if the panel has been closed. Is there a way to get to the HTML canvas element "isConnected" via ipywidgets, or ipycanvas, even if the panel has been closed?

martinRenou commented 3 years ago

Adding @jtpio to this discussion who might know the answer 😊

dsblank commented 3 years ago

For now, it looks like ipylab objects persist. I'll ask over there. Thanks!

jtpio commented 3 years ago

Ah I forgot to reply here!

for widget in app.shell.widgets.values():

Normally app.shell.widgets is not part of the public API yet. Or you meant accessing the "private" _widgets property?

dsblank commented 3 years ago

No, it was app.shell.widgets but I'll ask some follow up questions over on ipylab.

Screenshot from 2020-12-17 14-51-15