/ concept-collection / jupyterlite-numbl-kernel
Sign in
concept-collection / jupyterlite-numbl-kernel
jupyterlite-numbl-kernel / README.md
140 lines · 6.1 KBCodeBlameHistory
41c072cjupyterlite-numbl-kernel: MATLAB-syntax kernel for JupyterLiteJeremy Magland 1# jupyterlite-numbl-kernel
f44f6a3Rebrand numbl-first: this runs numbl (which uses MATLAB syntax)Jeremy Magland 3Run [**numbl**](https://github.com/flatironinstitute/numbl) in
4[JupyterLite](https://jupyterlite.readthedocs.io/) notebooks — **entirely in
5the browser**, with no server, no kernel process, and nothing for the reader
6to install.
f44f6a3Rebrand numbl-first: this runs numbl (which uses MATLAB syntax)Jeremy Magland 8numbl is an open-source numerical-computing engine, written in TypeScript,
9that uses MATLAB syntax — so `.m` code runs unchanged. This kernel runs a
41c072cjupyterlite-numbl-kernel: MATLAB-syntax kernel for JupyterLiteJeremy Magland 10numbl session in a Web Worker in the page: variables persist across cells,
f44f6a3Rebrand numbl-first: this runs numbl (which uses MATLAB syntax)Jeremy Magland 11console output streams into the running cell, plots render as figures in cell
12outputs (including interactive 3-D), the `mip` package manager can install
13numbl packages from GitHub, and `.m` files next to the notebook are part of
14the workspace (named functions, called from cells) — all client-side.
16**Demo site:**
17<https://concept-collection.github.io/jupyterlite-numbl-kernel/> (deployed
18from this repo via GitHub Pages — see `.github/workflows/deploy.yml`)
20## Why
f44f6a3Rebrand numbl-first: this runs numbl (which uses MATLAB syntax)Jeremy Magland 22This kernel is a proof of concept that a numbl notebook can be a static web
23page: hostable on GitHub Pages, shareable as a link, and executable by anyone
24with a browser. Because numbl uses MATLAB syntax and needs no MATLAB/Octave
25install (or any server process), the same `.m` code that would otherwise
26require a licensed product behind a server just runs in the tab.
28## How it works
30Three small pieces, all in this repo:
32- **Kernel** (`src/kernel.ts`) — implements JupyterLite's `BaseKernel` from
6b31a9aSync .m workspace files from the notebook directory into the sessionJeremy Magland 33 `@jupyterlite/services`. Before each `execute_request`, `.m` files in the
34 notebook's directory (read via the JupyterLite contents manager) are
35 synced into the numbl session; the cell source then runs against the
36 session's persistent workspace (`createNumblSession` /
37 `session.execute` from `numbl/browser`, a Web Worker that numbl manages).
38 Output streams back as `stream` messages; the run's plot instructions are
39 published as `display_data` with the mime type
40 `application/vnd.numbl.figure+json`.
41c072cjupyterlite-numbl-kernel: MATLAB-syntax kernel for JupyterLiteJeremy Magland 41- **Figure renderer** (`src/mime.tsx`) — a JupyterLab mime renderer for that
42 mime type: it replays the instructions through numbl's figures reducer and
43 mounts numbl's React `FigureView` (from `numbl/graphics`). Outputs are
44 plain JSON, so saved notebooks re-render wherever the extension is
45 installed.
46- **Kernel registration** (`src/index.ts`) — registers the kernelspec with
47 JupyterLite's `IKernelSpecs`.
49## Build a site with it
51```bash
52pip install jupyterlite-core jupyterlite-numbl-kernel
53jupyter lite build --contents my-notebooks --output-dir dist
54# dist/ is a static site — serve it anywhere
55```
57The `demo/` directory in this repo contains the demo site sources
58(notebooks + requirements); `.github/workflows/deploy.yml` builds and
59deploys it to GitHub Pages.
8befa62Demo: use in-memory JupyterLite storage so reloads show fresh contentJeremy Magland 61### Always-fresh content (demo choice)
63By default JupyterLite copies notebooks into the browser's IndexedDB on
64first visit, and that local copy then wins over the deployed files **even
65after a redeploy** — so returning visitors keep seeing stale content. Since
66this is a demo, `demo/jupyter-lite.json` opts into JupyterLite's in-memory
67storage so every page reload re-seeds the latest deployed notebooks:
69```json
71 "jupyter-config-data": {
72 "enableMemoryStorage": true,
73 "contentsStorageDrivers": ["memoryStorageDriver"],
74 "settingsStorageDrivers": ["memoryStorageDriver"],
75 "workspacesStorageDrivers": ["memoryStorageDriver"]
76 }
78```
80The trade-off is that a visitor's edits live only for the session and are
81discarded on reload. For a real deployment where users should keep their
82work, omit these keys (the default persistent storage) and bump
83`contentsStorageName` when you want to force-refresh shipped content.
84numbl's own package cache (installed via `mip`) lives in a separate
85IndexedDB store and is unaffected, so `mip`-installed packages still
86persist across reloads.
41c072cjupyterlite-numbl-kernel: MATLAB-syntax kernel for JupyterLiteJeremy Magland 88## Limitations (proof of concept)
90- **No interrupt**: a runaway cell can only be stopped by restarting the
91 kernel (restart works and gives a fresh workspace). Cooperative
92 cancellation exists in numbl but needs `SharedArrayBuffer`, i.e.
93 cross-origin isolation headers, which plain GitHub Pages doesn't set.
94- **No `input()`** (stdin), for the same reason.
95- **Figures are per-cell** (like inline matplotlib): each cell renders the
96 figures its own commands produce; `hold on` does not span cells.
97- **Named function definitions are not supported inside cells** (a numbl
98 REPL limitation) — anonymous functions work; named functions belong in
6b31a9aSync .m workspace files from the notebook directory into the sessionJeremy Magland 99 `.m` files next to the notebook (see `demo/content/statsutils.m`), which
100 this kernel syncs into the session automatically.
101- The `.m`-file sync is **one-way**: deleting a `.m` file from the file
102 browser leaves its function defined until the kernel restarts, and files
103 written by cell code (e.g. via `fopen`) don't appear back in the file
104 browser.
41c072cjupyterlite-numbl-kernel: MATLAB-syntax kernel for JupyterLiteJeremy Magland 105- **uihtml** components render display-only; the MATLAB↔HTML event bridge
106 is not wired into outputs yet.
107- numbl itself is not MATLAB: it covers a large, tested subset of the
108 language and toolbox surface. See the
109 [numbl repo](https://github.com/flatironinstitute/numbl) for scope.
111## Development
113Requires Python ≥ 3.9 and NodeJS ≥ 20, and `numbl >= 0.4.14` on npm (the
114first release with the incremental `session.execute` browser API). To
115develop against an unreleased numbl checkout, run `npm pack` there and
116point the `numbl` dependency at the tarball.
118```bash
119python -m venv .venv && source .venv/bin/activate
120pip install "jupyterlab~=4.6.0" "jupyterlite-core==0.8.1"
122jlpm install
123jlpm build # tsc + labextension (dev)
124pip install -e . # editable install, registers the labextension
126# Build and serve the demo site locally
127pip install -r demo/requirements.txt
128jupyter lite build --lite-dir demo --contents content --output-dir demo/_output
129python -m http.server -d demo/_output 8000
130```
132`jlpm watch` rebuilds on change during development.
134## License
136Apache-2.0. Built on [numbl](https://github.com/flatironinstitute/numbl) and
137the [JupyterLite](https://github.com/jupyterlite/jupyterlite) kernel API;
138scaffolding follows the
139[jupyterlite/echo-kernel](https://github.com/jupyterlite/echo-kernel)
140template.
moveopenescclose