Skip to content

current canvas out of sync after page reload #322

Open
@gagern

Description

While debugging some other issue, I've encountered an exception in CanvasManagerModel.processCommand caused by this.currentCanvas being undefined.

Steps to reproduce:

  1. Start new notebook.
  2. Enter and run cell from ipycanvas import Canvas, hold_canvas
  3. Enter and run cell canvas = Canvas(width=100, height=100)
  4. Enter and run cell canvas.fill_rect(10, 10, 20, 20)
  5. Re-load browser window
  6. Optional: Enable pause on exception in developer tools
  7. 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.

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions