Skip to content

Make the js backend a proper notebook backend - #14333

Merged
larsoner merged 30 commits into
mne-tools:mainfrom
larsoner:js-backend
Nov 30, 2026
Merged

larsoner merged 30 commits into
mne-tools:mainfrom
larsoner:js-backend

Conversation

@larsoner

Copy link
Copy Markdown
Member

Make the "jupyterlite" backend an actual interactive backend named notebook_js. Performance is not great because on changes the whole thing needs to be re-serialized, but I'm going to work on streamlining that at the pyvista-js end. Changes ended up being pretty simple, with the exception being due to this re-serialization -- we need to "double buffer", i.e., render to a new widget fully before swapping, rather than a bare replace in order to avoid flashing. And we need some JS to make it clear an interactive update is in progress but waiting on the serialization. In the meantime zooming and rotating etc. is faster than the notebook backend (and the abstraction helps our code readability actually I think).

This code:

import mne

mne.viz.set_3d_backend("notebook_js")
data_path = mne.datasets.sample.data_path()
stc = mne.read_source_estimate(
    data_path / "MEG" / "sample" / "sample_audvis-meg", subject="sample"
)
brain = stc.plot(
    subjects_dir=data_path / "subjects",
    hemi="lh",
    views="lat",
    initial_time=0.1,
    time_viewer=True,
)

in a Jupyter notebook gives:

Screenshot From 2026-09-23 15-56-45

Also fixes a little double-event-publish bug along the way.

Changes drafted with Claude Opus 5.5 (especially helping with the JS!) but understood and iterated on by me.

@larsoner
larsoner marked this pull request as draft September 24, 2026 11:57
@larsoner

Copy link
Copy Markdown
Member Author

Converting to draft since tkoyama010/pyvista-js#634 will take updates from ~2.4s to ~0.4s, and simplify things a little bit for us (we won't need the double-buffering anymore!). We'll require quite a new pyvista-js (because that needs to be merged then a release made) but I think that's okay.

@larsoner
larsoner force-pushed the js-backend branch 3 times, most recently from a787d77 to 615da3d Compare September 26, 2026 15:20
* upstream/main: (26 commits)
  Splitting _brain.py into multiple modules (phase 1) (mne-tools#14359)
  Fix small corner-case bug with moments (mne-tools#14364)
  Coregistration GUI modernisation (phase 5) (mne-tools#14353)
  FIX: Prevent memory leak in RawArray by removing data from _init_kwargs (mne-tools#14360)
  MAINT: Update code credit (mne-tools#14365)
  type-hint the rest of mne/_fiff (mne-tools#14352)
  MAINT: Update dependency specifiers (mne-tools#14362)
  MAINT: Update dependency specifiers (mne-tools#14357)
  Switch to mne-tools SPEC-0 action (mne-tools#14356)
  MAINT: Move Pixi file updates to autofix workflow (mne-tools#14345)
  BUG: Fix SNIRF landmark length unit scaling (mne-tools#14354)
  DRY dipole code and comment on implementation (mne-tools#14350)
  Speed up CSD and PSD computations (mne-tools#14355)
  [dependabot]: Bump the actions group with 4 updates (mne-tools#14349)
  MAINT: Update pre-commit hook versions (mne-tools#14347)
  Switch to `mne-tools/rename-towncrier` action (mne-tools#14344)
  Fix for latest pyvista-js (mne-tools#14343)
  Avoid copies and support larger files in EEGLAB export (mne-tools#14338)
  Remove line breaks in sponsors check issue text (mne-tools#14337)
  Fix EEGLAB events after dropped epochs (mne-tools#14067)
  ...

# Conflicts:
#	mne/viz/_brain/_brain.py
#	mne/viz/backends/tests/test_renderer.py
@larsoner
larsoner marked this pull request as ready for review October 7, 2026 17:02
@larsoner

larsoner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Okay this is ready! Can be tested with the "launch lite" button for the dSPM example, which links here and for me gives:

image

@drammock

drammock commented Oct 7, 2026

Copy link
Copy Markdown
Member

@natinew77-creator do you want to review this one?

@natinew77-creator

Copy link
Copy Markdown
Contributor

@natinew77-creator do you want to review this one?

Thanks for reaching out! I'd be happy to take a look. I'm currently busy with classes and midterm exams, but I'll review it as soon as I have some free time.

@natinew77-creator

Copy link
Copy Markdown
Contributor

Hi @drammock and @larsoner,

Sorry for the delayed review, I've been quite busy with classes and midterm exams!

I pulled the latest commits and tested everything locally with pyvista-js 0.21.0 on Python 3.12. The move to a single iframe with in-place _diff_page() updates is a really clean simplification over the earlier double-buffering approach, and decoupling _IpyRenderer to bring the full time viewer into notebook_js works seamlessly. All backend tests pass cleanly, including test_lite_brain.

Looks great to me, and I would be happy to see this merged!

@larsoner

Copy link
Copy Markdown
Member Author

Okay this was approved by @natinew77-creator and greatly improves the jupyterlite experience so I'll merge!

@larsoner
larsoner merged commit e3eba19 into mne-tools:main Nov 30, 2026
25 checks passed
@larsoner
larsoner deleted the js-backend branch November 30, 2026 17:07
@larsoner larsoner added the release-highlight Will be put in the highlight section of the release announcement label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-highlight Will be put in the highlight section of the release announcement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants