jupyter-widgets-contrib / ipycanvas

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

Give each canvas an explicit reference to the canvas manager it uses. #290

Closed jasongrout closed 2 years ago

jasongrout commented 2 years ago

Right now, there is an implicit, but not explict, link in the code between the canvas manager and the canvases, so any algorithm that tries to trace widget dependencies misses that the canvas manager is associated with each canvas. This makes that association explicit (as a bonus, allows a person to create multiple canvas managers if they so desire, I suppose).

This fixes a problem we have with ipycanvas in the Databricks notebook. In the Databricks notebook, we isolate widgets to individual iframes, and only create widget models for widgets that are explicitly referenced. Since the CanvasManager was not referenced from any canvas (only an implicit association in the Python code), we wouldn't ever create a CanvasManager frontend object in the iframe with the Canvas. This PR makes this association explicit, so algorithms (like ours) that rely on explicit associations between widgets to create them from a single root widget will work. This also helps in the algorithm we've thought about in the past to serialize a tree of widgets to a notebook, in that the algorithm can now find the correct explicitly associated CanvasManager object.

martinRenou commented 2 years ago

Thanks a lot @jasongrout ! I've been thinking about making this exact fix for Colab as well, but never took the time.