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