Description
While debugging some other issue, I've encountered an exception in CanvasManagerModel.processCommand
caused by this.currentCanvas
being undefined.
Steps to reproduce:
- Start new notebook.
- Enter and run cell
from ipycanvas import Canvas, hold_canvas
- Enter and run cell
canvas = Canvas(width=100, height=100)
- Enter and run cell
canvas.fill_rect(10, 10, 20, 20)
- Re-load browser window
- Optional: Enable pause on exception in developer tools
- Re-execute the last cell
Expected behavior:
Command executed cleanly, or a reasonably understandable exception raised in Python code and surfaced in the UI.
Actual behavior:
Error thrown: Cannot read properties of undefined
(reading 'fillRect
')
Hypothesis:
I believe this is most likely due to a mismatch of state. The Python _CanvasManager._current_canvas
refers to the last used canvas, but on page reload the JavaScript CanvasManagerModel
gets re-created and there is no machinery in place (as far as I can tell) to sync the current canvas between the two.
Potential solutions:
- Have the
CanvasManagerModel
widget constructor signal its Python counterpart to invalidate its_current_canvas_
state. That way the next draw operation would sync state again. - Use some IPyWidgets machinery to sync the current canvas state from Python to JavaScript upon widget recreation.
I don't know enough about IPyWidgets to judge which of these approaches to prefer, or how the latter would look in more detail.