Skip to content

Commit 87c8a24

Browse files
authored
Many improvements (#20)
* Many improvements * examples * lint * fix RTD * add missing jupyter-server * code style * fix some docstrings * more style fixes * code registry * fix * iframe + web worker * fixes * comlink and web worker example * type fixes * cleanup * fixes * lint * lint * fix display names * console fixes
1 parent 239a447 commit 87c8a24

24 files changed

Lines changed: 5308 additions & 367 deletions

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ jobs:
109109
name: extension-artifacts
110110
- name: Install the dependencies
111111
run: |
112-
python -m pip install --pre jupyterlite-core jupyterlite_javascript_kernel*.whl
112+
python -m pip install --pre jupyterlite-core jupyter-server jupyterlite_javascript_kernel*.whl
113113
- name: Build the JupyterLite site
114114
run: |
115-
jupyter lite build --output-dir dist
115+
jupyter lite build --output-dir dist --contents examples
116116
- name: Upload artifact
117117
uses: actions/upload-pages-artifact@v3
118118
with:

.readthedocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ build:
77
commands:
88
- mamba env update --name base --file docs/environment.yml
99
- python -m pip install .
10-
- jupyter lite build --output-dir dist
10+
- jupyter lite build --output-dir dist --contents examples
1111
- mkdir -p $READTHEDOCS_OUTPUT/html
1212
- cp -r dist/* $READTHEDOCS_OUTPUT/html/

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,115 @@ To remove the extension, execute:
2929
pip uninstall jupyterlite-javascript-kernel
3030
```
3131

32+
## Runtime modes
33+
34+
The extension currently registers two JavaScript kernelspecs:
35+
36+
- `JavaScript (IFrame)`:
37+
Runs code in a hidden runtime `iframe` on the main page thread. Use this when your code needs browser DOM APIs like `document`, `window`, or canvas access through the page context.
38+
- `JavaScript (Web Worker)`:
39+
Runs code in a dedicated Web Worker. Use this for stronger isolation and to avoid blocking the main UI thread.
40+
41+
Pick either kernel from the notebook kernel selector in JupyterLite.
42+
43+
### Worker mode limitations
44+
45+
Web Workers do not expose DOM APIs. In `JavaScript (Web Worker)`, APIs such as `document`, direct element access, and other main-thread-only browser APIs are unavailable.
46+
47+
### Import side effects in iframe mode
48+
49+
In `JavaScript (IFrame)`, user code and imports execute in the runtime iframe scope.
50+
51+
By default, module-level side effects stay in the runtime iframe. To intentionally affect the main page (`window.parent`), access it directly.
52+
53+
Cell declarations like `var`, `let`, `const`, `function`, and `class` remain in the runtime scope. Host-page mutations happen when your code (or imported code) explicitly reaches `window.parent`.
54+
55+
#### Example: canvas-confetti
56+
57+
```javascript
58+
import confetti from 'canvas-confetti';
59+
60+
const canvas = window.parent.document.createElement('canvas');
61+
Object.assign(canvas.style, {
62+
position: 'fixed',
63+
inset: '0',
64+
width: '100%',
65+
height: '100%',
66+
pointerEvents: 'none',
67+
zIndex: '2147483647'
68+
});
69+
window.parent.document.body.appendChild(canvas);
70+
71+
const fire = confetti.create(canvas, { resize: true, useWorker: true });
72+
73+
fire({ particleCount: 20, spread: 70 });
74+
```
75+
76+
#### Example: p5.js
77+
78+
```javascript
79+
import p5 from 'p5';
80+
81+
const mount = window.parent.document.createElement('div');
82+
Object.assign(mount.style, {
83+
position: 'fixed',
84+
right: '16px',
85+
bottom: '16px',
86+
zIndex: '1000'
87+
});
88+
window.parent.document.body.appendChild(mount);
89+
90+
const sketch = new p5(p => {
91+
p.setup = () => {
92+
p.createCanvas(120, 80);
93+
p.noLoop();
94+
};
95+
}, mount);
96+
```
97+
98+
#### Can side effects be auto-detected and cleaned up?
99+
100+
Partially, yes, but not perfectly. This project currently does not provide automatic side-effect cleanup for host-page mutations.
101+
102+
Limits of automatic cleanup:
103+
104+
- It will not reliably undo monkey-patched globals.
105+
- It will not automatically remove all event listeners or timers.
106+
- It cannot safely revert all stateful third-party module internals.
107+
108+
### Enable or disable specific modes
109+
110+
The two runtime modes are registered by separate plugins:
111+
112+
- `@jupyterlite/javascript-kernel-extension:kernel-iframe`
113+
- `@jupyterlite/javascript-kernel-extension:kernel-worker`
114+
115+
You can disable either one using `disabledExtensions` in `jupyter-config-data`.
116+
117+
Disable worker mode:
118+
119+
```json
120+
{
121+
"jupyter-config-data": {
122+
"disabledExtensions": [
123+
"@jupyterlite/javascript-kernel-extension:kernel-worker"
124+
]
125+
}
126+
}
127+
```
128+
129+
Disable iframe mode:
130+
131+
```json
132+
{
133+
"jupyter-config-data": {
134+
"disabledExtensions": [
135+
"@jupyterlite/javascript-kernel-extension:kernel-iframe"
136+
]
137+
}
138+
}
139+
```
140+
32141
## Contributing
33142

34143
### Development install

0 commit comments

Comments
 (0)