Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

jprochazk
Copy link
Member

@jprochazk jprochazk commented Jun 2, 2025

Related

What

We want to store the Wasm binary as a separate file, and let the Viewer load it via fetch at runtime. This leads to the best outcomes in terms of startup times.

Build changes

The rerun_notebook build has been updated to:

  • Copy the Wasm binary from node_modules to /static
  • Preserve special inline marker comments (//!<INLINE-MARKER> at the time of writing)

We manually copy the Wasm binary to the output directory, because any of my attempts to do this via esbuild failed. We just need the file there, so we do it manually.

We need to mark the start and end of a block of code that should be replaced with the inlined Wasm binary + the glue that loads it from a base64-encoded string. We use esbuild to bundle the notebook widget, and esbuild does not preserve regular comments. There are also no options to enable this behavior.
The exception is "legal comments" which are normally used for e.g. copyright information. The marker comments just happen to be something that esbuild considers a "legal comment".

The rerun-js/web-viewer build no longer combines the Wasm and JS into a single file - this is instead done at runtime, by the rerun_notebook package, becuase we don't want to publish two copies of the Wasm binary (one as its own file, one inlined into the JS widget) when publishing the package. It has a negligible startup cost, which is paid once at time of import rerun.notebook, and only if RERUN_NOTEBOOK_ASSET is set to inline.

The asset server has been updated to also serve the re_viewer_bg.wasm file. It serves it gzipped, with the right content-type so that it can be stream-compiled.

Check for assets

We now send a HEAD request to check if the assets we need are accessible on the browser side, by injecting a small JS snippet in parallel with the regular Viewer widget loading. This injection is done via a combination of ipywidgets.HTML and a base64-encoded javascript file inserted into a style tag's onload property.

It's done in this convoluted way because ipywidgets.HTML uses innerHTML to set the content of the element it creates. JS is not executed by running innerHTML (see https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#security_considerations)... except if it's in special properties which are treated as event listeners. It's base64-encoded so that the javascript we inject does not need to avoid using quotes or other symbols which may cause the resulting HTML string to not parse properly.

If the widget isn't accessible, we let the user know with a clear error message.

Additionally, the rerun_notebook import has been moved from inside the Viewer constructor to the top-level again. Despite rerun_notebook being an optional package, we've been able to do this without causing any issues for a while, because we no longer import the rerun.notebook package anywhere inside rerun-sdk eagerly. Something like blueprint _ipython_display_ impls import it inside the function body, at which point we expect the user to have the rerun_notebook package installed, because we expect _ipython_display_ to be called from the context of a notebook. If someone tries to call it outside of that context, that's their problem ¯\_(ツ)_/¯.

block_until_ready=False by default

If the widget asset was not accessible for whatever reason, the widget would never finish loading without any error messages, unless you opened the browser devtools console. That was a super frustrating experience, so we instead set a hard limit (10 seconds) for how long the Viewer should take to load, and blocked cell execution until it did. Those 10 second turn out to just not be enough in many environments.

Because we now check if the required assets are accessible in the browser, we no longer need this, as we won't ever timeout in the same way. We already buffer all input data before the widget has loaded, so there is no issue with letting cell execution continue while the widget is loading.

jprochazk added 5 commits June 2, 2025 15:05
we don't import the rerun.notebook module anywhere inside
the sdk package, so it's totally fine to do this when
the user first imports rerun.notebook, instead of waiting
for when they first construct Viewer. the logs make more
sense this way, too.
The `rerun_notebook` build has been updated to:
* Copy the Wasm binary from node_modules to `/static`
* Preserve special inline marker comments

The asset server has been updated to also serve the `re_viewer_bg.wasm`
file. It serves it gzipped, with the right `content-type` so that it can
be stream-compiled.

The `rerun-js/web-viewer` build no longer combines the Wasm and JS into
a single file - this is instead done at _runtime_, by the
`rerun_notebook` package, becuase we don't want to publish two copies of
the Wasm binary (one as its own file, one inlined into the JS widget)
when publishing the package. It has a negligible startup cost, which is
paid once at time of `import rerun.notebook`, and only if
`RERUN_NOTEBOOK_ASSET` is set to `inline`.
Sending an HTTP request during import is... questionable, but it's just
a HEAD request, so the time it takes is equal to the user's latency to
the server, which is nothing compared to how long it takes for the
jupyter kernel to start doing something when you run the first cell.
The problem this was attempting to solve was the widget asset not being
accessible for whatever reason causing the widget to never finish
loading without any error messages, unless you opened the browser
devtools console. That was a super frustrating experience, so we instead
set a hard limit (10 seconds) for how long the Viewer should take to
load. That turns out to just not be enough in many environments...

Because we now check if the required assets are accessible during import
time, we no longer need this, as we won't ever timeout in the same way.
@jprochazk jprochazk added 🕸️ web regarding running the viewer in a browser sdk-python Python logging API 📉 performance Optimization, memory use, etc notebook Jupyter notebooks etc labels Jun 2, 2025
Copy link

github-actions bot commented Jun 2, 2025

Latest documentation preview deployed successfully.

Result Commit Link
5f4238d https://landing-89dlwco2c-rerun.vercel.app/docs

Note: This comment is updated whenever you push a commit.

Copy link

github-actions bot commented Jun 2, 2025

Web viewer built successfully. If applicable, you should also test it:

  • I have tested the web viewer
Result Commit Link Manifest
5f4238d https://rerun.io/viewer/pr/10111 +nightly +main

Note: This comment is updated whenever you push a commit.

@jprochazk jprochazk marked this pull request as draft June 2, 2025 15:03
@jprochazk
Copy link
Member Author

Current status: Investigating why "restart kernel and run all cells" does not work. Viewers show up, but receive no data. Others don't show up at all. At least the notebook doesn't get stuck like on main

@jprochazk jprochazk changed the title Improve rerun_notebook startup times Improve rerun_notebook startup times and reliability Jun 3, 2025
@jprochazk jprochazk changed the title Improve rerun_notebook startup times and reliability Improve rerun_notebook startup times Jun 6, 2025
@jprochazk jprochazk marked this pull request as ready for review June 6, 2025 10:54
@jprochazk
Copy link
Member Author

Decided to leave that as a separate task, because for me it's also broken on main.

@jprochazk
Copy link
Member Author

Opened an issue for the 2nd part:

@jprochazk
Copy link
Member Author

image

@jprochazk jprochazk requested a review from jleibs June 13, 2025 13:47
@jprochazk jprochazk added this to the 0.24.0 milestone Jun 13, 2025
@emilk
Copy link
Member

emilk commented Jun 16, 2025

@jleibs do you still intend to review this?

@jleibs
Copy link
Member

jleibs commented Jun 16, 2025

@jleibs do you still intend to review this?

lol, looks like I was actively re-reviewing when you asked.

@jprochazk jprochazk merged commit 1a572b5 into main Jun 17, 2025
47 checks passed
@jprochazk jprochazk deleted the jan/widget-timeouts branch June 17, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
include in changelog notebook Jupyter notebooks etc 📉 performance Optimization, memory use, etc sdk-python Python logging API 🕸️ web regarding running the viewer in a browser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Eliminate notebook widget timeouts
3 participants