Skip to content

Add anywidget backend#200

Open
almarklein wants to merge 6 commits intomainfrom
anywidget
Open

Add anywidget backend#200
almarklein wants to merge 6 commits intomainfrom
anywidget

Conversation

@almarklein
Copy link
Copy Markdown
Member

@almarklein almarklein commented Mar 25, 2026

This adds a backend based on anywidget.

It is very similar to the jupyter backend (which uses jupyter_rfb), but it has no dependencies other than anywidget. This makes it more readily available.

The code is quite similar to jupyter_rfb:

  • Both use renderview.js (and we already have that JS module for pyodide)
  • The renderview-rfb.js is also shared.
  • The model uses the same traits, but in the anywidet backend they are private.

The main differences:

  • A simpler Python implementation, see next points.
  • Tighter coupling because the jupyter_rfb abstraction does not sit in between.
  • No output context to try to fix the problem of jupyter ignoring sys.stdout and exceptions raised in comm handlers. We don't need it, because our comm/trait handlers feed into the scheduling and only touch user code in fresh asyncio tasks.
  • No snapshot utility -> on second thought I think I want to add that too.

@almarklein
Copy link
Copy Markdown
Member Author

@kushalkolal do you have an idea to what degree people make use of jupyter_rgb's snapshot utility?

If this new backend becomes the default for notebooks, these people can still explicitly import the jupyter backend, but I'm not sure how much havoc that will cause :)

@almarklein almarklein marked this pull request as ready for review March 25, 2026 14:32
@almarklein
Copy link
Copy Markdown
Member Author

Another idea: rename the jupyter backend to the jupyter_rfb backend, to make it more explicit what the backend does.

@kushalkolar
Copy link
Copy Markdown
Contributor

@kushalkolal do you have an idea to what degree people make use of jupyter_rgb's snapshot utility?

If this new backend becomes the default for notebooks, these people can still explicitly import the jupyter backend, but I'm not sure how much havoc that will cause :)

In the fastplotlib notebook test we actually use snapshot, but the notebook test has been broken for a while with the latest rendercanvas changes and I will fix them after all this is settled.

Perhaps the havoc is worth it since it's a major improvement?

@FlynnOConnell
Copy link
Copy Markdown

@almarklein FWIW I don't have anyone using rfb snapshots and I don't use them myself

@almarklein
Copy link
Copy Markdown
Member Author

almarklein commented Mar 26, 2026

For the record, as part of all this work, I also refactored jupyter_rfb to use anywidget and renderview.js. But jupyter_rfb has become much 'thinner': no build step, most JS delegated. So it makes sense (I think) to just implement it here directly (and maybe also in Vispy).

The advantages are mainly:

  • No extra dependencies.
    • if someone installs rendercanvas, and already uses a notebook, chances are big they have anywidget installed, so things Just Work.
    • Not sure how big this advantage is in practice, but it could simplify downstream docs and pyproject files.
  • This implementation is a bit simpler than jupyter_rfb.
  • We want to experiment with JPEG/MPEG encoding on the GPU, so we'd need specialized JS for the decoding. It's much easier to add that to our own backend than to jupyter_rfb, also since this feature would be WGPU specific.

One issue is that there are now two backends for notebooks, which may cause some confusion. I'd propose to make this the new default and keep the jupyter_rfb backend for a while in case people specifically want to use it.

Regarding snapshots, I've thought about it some more, and I think I want to keep them (i.e. port them to this PR), but in a somewhat different form than jupyter_rfb currently does. See vispy/jupyter_rfb#146

@FlynnOConnell
Copy link
Copy Markdown

See vispy/jupyter_rfb#146

As you discuss here, the snapshot has been a useful placeholder saying "there are outputs here, you just can't view them in this context". I have been taking this feature for granted ...

In this way, ppl can use the canvas interactively, but also easily build tutorials with it that can be statically viewed.

If this would allow the snapshot to include a small interactive video, that would be immensely useful for making tutorials where I can just render videos directly from the notebook outputs rather than saving a video and rendering it with sphinx.

@almarklein
Copy link
Copy Markdown
Member Author

f this would allow the snapshot to include a small interactive video, that would be immensely useful

mmm ... apng ... 🤔

@FlynnOConnell
Copy link
Copy Markdown

lol I saw mpeg and thought maybe we could make the snapshot a movie... I guess that would then be several snapshots

@almarklein
Copy link
Copy Markdown
Member Author

Well, images can be embedded like <img src='data:image/image/jpeg;base64, ...'>, and I'm pretty sure that will work with apng (animated png), but for mpeg/mov we'd need a file, so that'd be much harder.

Maybe .snapshot(1) will record for 1 second 😄. I think it's possible, but would require some interesting tricks to buildup an apng during rendering ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants