@tinyvolt/pie 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -1
- package/dist/components/SplitPane.d.ts +21 -0
- package/dist/components/SplitPane.jsx +113 -0
- package/dist/components/icons.d.ts +12 -0
- package/dist/components/icons.jsx +45 -0
- package/dist/lib/autodiff/index.d.ts +28 -0
- package/dist/lib/autodiff/index.js +39 -0
- package/dist/lib/customModules.d.ts +19 -0
- package/dist/lib/customModules.js +52 -0
- package/dist/lib/markdown/compile.d.ts +27 -0
- package/dist/lib/markdown/compile.js +26 -0
- package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
- package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
- package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
- package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
- package/dist/lib/markdown/components/PiePage.d.ts +14 -0
- package/dist/lib/markdown/components/PiePage.jsx +42 -0
- package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
- package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
- package/dist/lib/markdown/features/build.d.ts +60 -0
- package/dist/lib/markdown/features/build.js +36 -0
- package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
- package/dist/lib/markdown/features/glossary/View.jsx +62 -0
- package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
- package/dist/lib/markdown/features/glossary/build.js +81 -0
- package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
- package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
- package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
- package/dist/lib/markdown/features/sidebar/build.js +7 -0
- package/dist/lib/markdown/features/tags/build.d.ts +7 -0
- package/dist/lib/markdown/features/tags/build.js +22 -0
- package/dist/lib/markdown/features/tags/url.d.ts +6 -0
- package/dist/lib/markdown/features/tags/url.js +10 -0
- package/dist/lib/markdown/features/toc/build.d.ts +7 -0
- package/dist/lib/markdown/features/toc/build.js +62 -0
- package/dist/lib/markdown/features/views.d.ts +4 -0
- package/dist/lib/markdown/features/views.jsx +11 -0
- package/dist/lib/markdown/frontmatter.d.ts +44 -0
- package/dist/lib/markdown/frontmatter.js +109 -0
- package/dist/lib/markdown/render.d.ts +6 -0
- package/dist/lib/markdown/render.js +95 -0
- package/dist/lib/nodes/animation.d.ts +14 -0
- package/dist/lib/nodes/animation.js +34 -0
- package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
- package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
- package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
- package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
- package/dist/lib/nodes/execute.d.ts +27 -0
- package/dist/lib/nodes/execute.js +159 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
- package/dist/lib/nodes/functions/array.d.ts +12 -0
- package/dist/lib/nodes/functions/array.js +31 -0
- package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
- package/dist/lib/nodes/functions/autodiff.js +60 -0
- package/dist/lib/nodes/functions/basic.d.ts +111 -0
- package/dist/lib/nodes/functions/basic.js +275 -0
- package/dist/lib/nodes/functions/registry.d.ts +9 -0
- package/dist/lib/nodes/functions/registry.js +45 -0
- package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
- package/dist/lib/nodes/functions/sideEffects.js +201 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
- package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
- package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
- package/dist/lib/nodes/jax.d.ts +46 -0
- package/dist/lib/nodes/jax.js +120 -0
- package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
- package/dist/lib/nodes/render/renderLogic.jsx +234 -0
- package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
- package/dist/lib/nodes/state/NameGenerator.js +29 -0
- package/dist/lib/nodes/state/Node.d.ts +134 -0
- package/dist/lib/nodes/state/Node.js +57 -0
- package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
- package/dist/lib/nodes/state/NodeStore.js +138 -0
- package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
- package/dist/lib/nodes/tensors/broadcast.js +92 -0
- package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
- package/dist/lib/nodes/tensors/tensor.js +34 -0
- package/dist/lib/python/lift.d.ts +51 -0
- package/dist/lib/python/lift.js +155 -0
- package/dist/lib/python/py/pie.py +303 -0
- package/dist/lib/python/py/pie_mpl.py +15 -0
- package/dist/lib/python/py/show_figures.py +5 -0
- package/dist/lib/python/runPage.d.ts +21 -0
- package/dist/lib/python/runPage.js +60 -0
- package/dist/lib/python/runner.d.ts +68 -0
- package/dist/lib/python/runner.js +243 -0
- package/dist/lib/python/worker.d.ts +1 -0
- package/dist/lib/python/worker.js +111 -0
- package/dist/lib/ui/UiLayer.d.ts +2 -0
- package/dist/lib/ui/UiLayer.jsx +15 -0
- package/dist/lib/ui/UiView.d.ts +23 -0
- package/dist/lib/ui/UiView.jsx +56 -0
- package/dist/lib/ui/components/checkboxes.d.ts +10 -0
- package/dist/lib/ui/components/checkboxes.jsx +30 -0
- package/dist/lib/ui/components/latex.d.ts +8 -0
- package/dist/lib/ui/components/latex.jsx +41 -0
- package/dist/lib/ui/components/radio.d.ts +6 -0
- package/dist/lib/ui/components/radio.jsx +19 -0
- package/dist/lib/ui/components/slider.d.ts +8 -0
- package/dist/lib/ui/components/slider.jsx +7 -0
- package/dist/lib/ui/components/textbox.d.ts +5 -0
- package/dist/lib/ui/components/textbox.jsx +21 -0
- package/dist/lib/ui/dragUi.d.ts +10 -0
- package/dist/lib/ui/dragUi.js +129 -0
- package/dist/lib/ui/nodeHandle.d.ts +15 -0
- package/dist/lib/ui/nodeHandle.js +37 -0
- package/dist/lib/ui/registry.d.ts +10 -0
- package/dist/lib/ui/registry.js +42 -0
- package/dist/runtime.css +696 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.jsx +7 -0
- package/init/index.js +2 -2
- package/init/site-agents.md +2 -1
- package/package.json +15 -6
- package/runtime-agents.md +168 -0
- package/src/app.css +7 -665
- package/src/lib/customModules.ts +67 -0
- package/src/lib/markdown/compile.ts +49 -0
- package/src/lib/markdown/components/PiePage.tsx +49 -0
- package/src/lib/markdown/features/build.ts +15 -6
- package/src/lib/markdown/features/sidebar/View.tsx +4 -2
- package/src/lib/markdown/features/tags/build.ts +6 -3
- package/src/lib/markdown/pages.ts +1 -9
- package/src/lib/markdown/vitePlugin.ts +4 -8
- package/src/lib/nodes/functions/registry.ts +30 -21
- package/src/lib/nodes/functions/sideEffects.ts +14 -8
- package/src/lib/nodes/functions/vitePlugin.ts +8 -10
- package/src/lib/nodes/state/Node.ts +27 -19
- package/src/lib/python/runPage.ts +78 -0
- package/src/lib/python/runner.ts +189 -164
- package/src/lib/siteModules.ts +13 -0
- package/src/lib/ui/dragUi.ts +4 -2
- package/src/lib/ui/registry.ts +35 -14
- package/src/page.css +664 -0
- package/src/routes/[...slug].tsx +1 -0
- package/src/runtime.css +29 -0
- package/src/runtime.tsx +16 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Page } from '../markdown/compile';
|
|
2
|
+
import { type CustomModules } from '../customModules';
|
|
3
|
+
export interface RunPageOptions {
|
|
4
|
+
/** Stop after this many milliseconds in total, including loading Python. Default 30000. */
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
/** The host's own functions, node types and UIs, as `<PiePage modules>` takes them. */
|
|
7
|
+
modules?: CustomModules;
|
|
8
|
+
}
|
|
9
|
+
/** One block that ran. */
|
|
10
|
+
export interface BlockResult {
|
|
11
|
+
/** What the block printed. */
|
|
12
|
+
output: string;
|
|
13
|
+
/** The Python error, or `Timed out after N s`. Unset when the block ran without one. */
|
|
14
|
+
error?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Runs `page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing them.
|
|
18
|
+
* Stops at the first block with an error. Runs in its own Python worker, which it terminates at the end.
|
|
19
|
+
* Rejects when a mounted PiePage has other `modules`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function runPage(page: Page, { timeoutMs, modules }?: RunPageOptions): Promise<BlockResult[]>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { holdCustomModules } from '../customModules';
|
|
2
|
+
import { NodeStore } from '../nodes/state/NodeStore';
|
|
3
|
+
import { checkUi } from '../ui/UiView';
|
|
4
|
+
import { PythonRunner, PythonScope } from './runner';
|
|
5
|
+
/**
|
|
6
|
+
* Runs `page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing them.
|
|
7
|
+
* Stops at the first block with an error. Runs in its own Python worker, which it terminates at the end.
|
|
8
|
+
* Rejects when a mounted PiePage has other `modules`.
|
|
9
|
+
*/
|
|
10
|
+
export async function runPage(page, { timeoutMs = 30_000, modules = {} } = {}) {
|
|
11
|
+
const doc = new DOMParser().parseFromString(page.html, 'text/html');
|
|
12
|
+
// The blocks `MarkdownBody` turns into editors.
|
|
13
|
+
const codes = Array.from(doc.querySelectorAll('pre > code.language-python-editor'), (el) => el.textContent ?? '');
|
|
14
|
+
const release = holdCustomModules(modules, "runPage: modules differ from the open PiePage's");
|
|
15
|
+
const runner = new PythonRunner();
|
|
16
|
+
const store = new NodeStore('right', 'none');
|
|
17
|
+
const scope = new PythonScope(store, runner);
|
|
18
|
+
const results = [];
|
|
19
|
+
let timedOut = false;
|
|
20
|
+
const timer = setTimeout(() => {
|
|
21
|
+
timedOut = true;
|
|
22
|
+
runner.kill();
|
|
23
|
+
}, timeoutMs);
|
|
24
|
+
try {
|
|
25
|
+
for (const code of codes) {
|
|
26
|
+
if (timedOut)
|
|
27
|
+
break;
|
|
28
|
+
const result = { output: '' };
|
|
29
|
+
results.push(result);
|
|
30
|
+
await new Promise((resolve) => {
|
|
31
|
+
scope.run(code, (status, error) => {
|
|
32
|
+
if (status === 'finished') {
|
|
33
|
+
if (error)
|
|
34
|
+
result.error = error;
|
|
35
|
+
resolve();
|
|
36
|
+
}
|
|
37
|
+
else if (status === 'killed') {
|
|
38
|
+
result.error = `Timed out after ${timeoutMs / 1000} s`;
|
|
39
|
+
resolve();
|
|
40
|
+
}
|
|
41
|
+
}, (text) => {
|
|
42
|
+
result.output += text;
|
|
43
|
+
}, () => { },
|
|
44
|
+
// Raises in Python as on a page; the UI isn't drawn.
|
|
45
|
+
(call) => checkUi(store, call));
|
|
46
|
+
});
|
|
47
|
+
if (result.error !== undefined)
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
runner.kill();
|
|
54
|
+
scope.dispose();
|
|
55
|
+
for (const id in store.nodes)
|
|
56
|
+
store.nodes[id]?.dispose?.();
|
|
57
|
+
release();
|
|
58
|
+
}
|
|
59
|
+
return results;
|
|
60
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { NodeStore } from '../nodes/state/NodeStore';
|
|
2
|
+
import { type InputValue, type PyResult } from './lift';
|
|
3
|
+
import type { UiCall } from '../ui/UiView';
|
|
4
|
+
export type RunStatus = 'waiting' | 'loading' | 'running' | 'finished' | 'stopped' | 'killed';
|
|
5
|
+
interface Run {
|
|
6
|
+
scope: PythonScope;
|
|
7
|
+
code: string;
|
|
8
|
+
onStatus: (status: RunStatus, error?: string) => void;
|
|
9
|
+
onOutput: (text: string) => void;
|
|
10
|
+
/** `png`: a matplotlib figure as a base64 PNG. */
|
|
11
|
+
onPlot: (png: string) => void;
|
|
12
|
+
/** A `pie.ui.*` call. Throws when the call is wrong; Python raises the error. */
|
|
13
|
+
onUi: (call: UiCall) => void;
|
|
14
|
+
}
|
|
15
|
+
/** A request to rerun the `pie.lift`ed function of node `nodeId` with new input values. */
|
|
16
|
+
interface Rerun {
|
|
17
|
+
scope: PythonScope;
|
|
18
|
+
nodeId: string;
|
|
19
|
+
values: InputValue[];
|
|
20
|
+
done: (result?: PyResult, error?: string) => void;
|
|
21
|
+
}
|
|
22
|
+
/** One Python worker and its queue of runs, which it runs one at a time. */
|
|
23
|
+
export declare class PythonRunner {
|
|
24
|
+
private worker;
|
|
25
|
+
private loaded;
|
|
26
|
+
private active;
|
|
27
|
+
private readonly queue;
|
|
28
|
+
private readonly waitingReruns;
|
|
29
|
+
private readonly sentReruns;
|
|
30
|
+
private nextRerunId;
|
|
31
|
+
private batchDone;
|
|
32
|
+
private onMessage;
|
|
33
|
+
private startNext;
|
|
34
|
+
private finish;
|
|
35
|
+
/**
|
|
36
|
+
* Sends the waiting reruns, unless a block is running. After a kill they wait for the next worker,
|
|
37
|
+
* which answers that Python was restarted.
|
|
38
|
+
*/
|
|
39
|
+
private sendReruns;
|
|
40
|
+
private killWorker;
|
|
41
|
+
/** Kills Python. The running run and every queued run end as killed. */
|
|
42
|
+
kill(): void;
|
|
43
|
+
/** Queues `run`. Returns its stop function. */
|
|
44
|
+
enqueue(run: Run): () => void;
|
|
45
|
+
/** Queues `rerun`. It waits while a block runs. */
|
|
46
|
+
rerun(rerun: Rerun): void;
|
|
47
|
+
/** Drops `scope`'s queued runs and reruns, and frees its variables. */
|
|
48
|
+
dispose(scope: PythonScope): void;
|
|
49
|
+
}
|
|
50
|
+
/** Runs that share Python variables, and the store their `pie.*` calls draw on. */
|
|
51
|
+
export declare class PythonScope {
|
|
52
|
+
readonly store?: NodeStore | undefined;
|
|
53
|
+
private readonly runner;
|
|
54
|
+
readonly id: string;
|
|
55
|
+
disposed: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* `store`: where `pie.*` calls draw. Without it they fail with "this page has no canvas".
|
|
58
|
+
* `runner`: the worker to run in. Default: the one every page shares.
|
|
59
|
+
*/
|
|
60
|
+
constructor(store?: NodeStore | undefined, runner?: PythonRunner);
|
|
61
|
+
/** Queues `code`. Returns a stop function. `error` comes only with `finished`. */
|
|
62
|
+
run(code: string, onStatus: Run['onStatus'], onOutput: Run['onOutput'], onPlot: Run['onPlot'], onUi: Run['onUi']): () => void;
|
|
63
|
+
/** Queues a rerun of node `nodeId`'s `pie.lift`ed function. It waits while a block runs. */
|
|
64
|
+
rerun(nodeId: string, values: InputValue[], done: Rerun['done']): void;
|
|
65
|
+
/** Frees the variables. Call when the page goes away. */
|
|
66
|
+
dispose(): void;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { execute } from '../nodes/execute';
|
|
2
|
+
import { getItem } from '../nodes/tensors/tensor';
|
|
3
|
+
import { prepareLifts, saveLift, startLift } from './lift';
|
|
4
|
+
let nextScopeId = 0;
|
|
5
|
+
const message = (err) => (err instanceof Error ? err.message : String(err));
|
|
6
|
+
/** One Python worker and its queue of runs, which it runs one at a time. */
|
|
7
|
+
export class PythonRunner {
|
|
8
|
+
worker;
|
|
9
|
+
loaded = false;
|
|
10
|
+
active;
|
|
11
|
+
queue = [];
|
|
12
|
+
// Reruns waiting for the running block to end, by scope and node id. A newer request replaces a waiting one.
|
|
13
|
+
waitingReruns = new Map();
|
|
14
|
+
// Reruns sent to the worker, by request id.
|
|
15
|
+
sentReruns = new Map();
|
|
16
|
+
nextRerunId = 0;
|
|
17
|
+
// Set while a `pie.*` call waits for Python to run a batch: takes Python's results and the id of its request.
|
|
18
|
+
batchDone;
|
|
19
|
+
onMessage(from, { data }) {
|
|
20
|
+
const run = this.active;
|
|
21
|
+
if (data.type === 'loaded') {
|
|
22
|
+
this.loaded = true;
|
|
23
|
+
run?.onStatus('running');
|
|
24
|
+
}
|
|
25
|
+
if (data.type === 'call' || data.type === 'item' || data.type === 'get_node' || data.type === 'lift' || data.type === 'ui') {
|
|
26
|
+
// The request to answer: this one, or, after a batch, Python's request with the batch's results.
|
|
27
|
+
let reqId = data.reqId;
|
|
28
|
+
// A reply from before a kill must not reach the new worker.
|
|
29
|
+
const reply = (msg) => {
|
|
30
|
+
if (this.worker === from)
|
|
31
|
+
from.postMessage({ type: 'reply', reqId, ...msg });
|
|
32
|
+
};
|
|
33
|
+
const store = run?.scope.store;
|
|
34
|
+
if (!store) {
|
|
35
|
+
reply({ error: 'this page has no canvas' });
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (data.type === 'item') {
|
|
39
|
+
try {
|
|
40
|
+
reply({ result: getItem(store, data.arrayId, data.position) });
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
reply({ error: message(err) });
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (data.type === 'get_node') {
|
|
48
|
+
const node = store.getNodeValue(data.id);
|
|
49
|
+
if (!node)
|
|
50
|
+
reply({ error: `pie.get_node: no node with id '${data.id}'` });
|
|
51
|
+
else if (node.type === 'Array')
|
|
52
|
+
reply({ result: { id: node.id, type: node.type, shape: node.shape, itemType: node.itemType } });
|
|
53
|
+
else
|
|
54
|
+
reply({ result: { id: node.id, type: node.type } });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (data.type === 'ui') {
|
|
58
|
+
try {
|
|
59
|
+
run.onUi({ name: data.name, args: data.args, options: data.options });
|
|
60
|
+
reply({ result: null });
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
reply({ error: message(err) });
|
|
64
|
+
}
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (data.type === 'lift') {
|
|
68
|
+
try {
|
|
69
|
+
reply({ result: startLift(store, data.name, data.inputIds, data.kind, data.out, data.fill, data.stroke) });
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
reply({ error: message(err) });
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
// Python runs a batch inside this call: the reply asks for it, and Python sends the results back as a new request.
|
|
77
|
+
const runBatch = (jobs) => new Promise((resolve) => {
|
|
78
|
+
this.batchDone = (next, results) => {
|
|
79
|
+
reqId = next;
|
|
80
|
+
resolve(results);
|
|
81
|
+
};
|
|
82
|
+
reply({ result: { batch: jobs } });
|
|
83
|
+
});
|
|
84
|
+
execute(store, data.keyword, data.args, data.options, (visitSteps) => prepareLifts(visitSteps, runBatch)).then((result) => reply({ result }), (err) => reply({ error: message(err) }));
|
|
85
|
+
}
|
|
86
|
+
if (data.type === 'batch_done') {
|
|
87
|
+
const done = this.batchDone;
|
|
88
|
+
this.batchDone = undefined;
|
|
89
|
+
done?.(data.reqId, data.results);
|
|
90
|
+
}
|
|
91
|
+
// No reply: Python doesn't wait for it.
|
|
92
|
+
if (data.type === 'lift_save') {
|
|
93
|
+
const scope = run?.scope;
|
|
94
|
+
try {
|
|
95
|
+
if (scope?.store)
|
|
96
|
+
saveLift(scope.store, data, (nodeId, values, done) => scope.rerun(nodeId, values, done));
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
console.error(`${data.name}: ${message(err)}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (data.type === 'rerun_done') {
|
|
103
|
+
const rerun = this.sentReruns.get(data.reqId);
|
|
104
|
+
this.sentReruns.delete(data.reqId);
|
|
105
|
+
rerun?.done(data.result, data.error);
|
|
106
|
+
}
|
|
107
|
+
if (data.type === 'output') {
|
|
108
|
+
run?.onOutput(data.text);
|
|
109
|
+
}
|
|
110
|
+
if (data.type === 'plot') {
|
|
111
|
+
run?.onPlot(data.png);
|
|
112
|
+
}
|
|
113
|
+
if (data.type === 'done') {
|
|
114
|
+
this.finish(data.error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
startNext() {
|
|
118
|
+
if (this.active || this.queue.length === 0)
|
|
119
|
+
return;
|
|
120
|
+
this.active = this.queue.shift();
|
|
121
|
+
if (!this.worker) {
|
|
122
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
|
|
123
|
+
worker.onmessage = (e) => this.onMessage(worker, e);
|
|
124
|
+
worker.onerror = (e) => this.finish(e.message || 'Python worker failed to start');
|
|
125
|
+
this.worker = worker;
|
|
126
|
+
}
|
|
127
|
+
this.active.onStatus(this.loaded ? 'running' : 'loading');
|
|
128
|
+
this.worker.postMessage({ type: 'run', scope: this.active.scope.id, code: this.active.code });
|
|
129
|
+
}
|
|
130
|
+
finish(error) {
|
|
131
|
+
const run = this.active;
|
|
132
|
+
if (!run)
|
|
133
|
+
return;
|
|
134
|
+
this.active = undefined;
|
|
135
|
+
// Pyodide failed to load: start a fresh worker next time instead of reusing the broken one.
|
|
136
|
+
if (!this.loaded)
|
|
137
|
+
this.killWorker();
|
|
138
|
+
if (run.scope.disposed)
|
|
139
|
+
this.worker?.postMessage({ type: 'dispose', scope: run.scope.id });
|
|
140
|
+
run.onStatus('finished', error);
|
|
141
|
+
this.sendReruns();
|
|
142
|
+
this.startNext();
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Sends the waiting reruns, unless a block is running. After a kill they wait for the next worker,
|
|
146
|
+
* which answers that Python was restarted.
|
|
147
|
+
*/
|
|
148
|
+
sendReruns() {
|
|
149
|
+
if (this.active || !this.worker)
|
|
150
|
+
return;
|
|
151
|
+
for (const rerun of this.waitingReruns.values()) {
|
|
152
|
+
const reqId = this.nextRerunId++;
|
|
153
|
+
this.sentReruns.set(reqId, rerun);
|
|
154
|
+
this.worker.postMessage({ type: 'rerun', reqId, scope: rerun.scope.id, nodeId: rerun.nodeId, values: rerun.values });
|
|
155
|
+
}
|
|
156
|
+
this.waitingReruns.clear();
|
|
157
|
+
}
|
|
158
|
+
killWorker() {
|
|
159
|
+
this.worker?.terminate();
|
|
160
|
+
this.worker = undefined;
|
|
161
|
+
this.loaded = false;
|
|
162
|
+
// Their replies will never come.
|
|
163
|
+
this.sentReruns.clear();
|
|
164
|
+
this.batchDone = undefined;
|
|
165
|
+
}
|
|
166
|
+
/** Kills Python. The running run and every queued run end as killed. */
|
|
167
|
+
kill() {
|
|
168
|
+
this.killWorker();
|
|
169
|
+
const ended = [...(this.active ? [this.active] : []), ...this.queue];
|
|
170
|
+
this.active = undefined;
|
|
171
|
+
this.queue.length = 0;
|
|
172
|
+
for (const run of ended)
|
|
173
|
+
run.onStatus('killed');
|
|
174
|
+
}
|
|
175
|
+
/** Queues `run`. Returns its stop function. */
|
|
176
|
+
enqueue(run) {
|
|
177
|
+
this.queue.push(run);
|
|
178
|
+
run.onStatus('waiting');
|
|
179
|
+
this.startNext();
|
|
180
|
+
// The stop function (the editor's ■ button): drops the run if queued, kills Python if it's running.
|
|
181
|
+
return () => {
|
|
182
|
+
const at = this.queue.indexOf(run);
|
|
183
|
+
if (at !== -1) {
|
|
184
|
+
this.queue.splice(at, 1);
|
|
185
|
+
run.onStatus('stopped');
|
|
186
|
+
}
|
|
187
|
+
else if (this.active === run) {
|
|
188
|
+
this.kill();
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/** Queues `rerun`. It waits while a block runs. */
|
|
193
|
+
rerun(rerun) {
|
|
194
|
+
this.waitingReruns.set(`${rerun.scope.id}/${rerun.nodeId}`, rerun);
|
|
195
|
+
this.sendReruns();
|
|
196
|
+
}
|
|
197
|
+
/** Drops `scope`'s queued runs and reruns, and frees its variables. */
|
|
198
|
+
dispose(scope) {
|
|
199
|
+
for (let i = this.queue.length - 1; i >= 0; i--) {
|
|
200
|
+
if (this.queue[i].scope === scope)
|
|
201
|
+
this.queue.splice(i, 1);
|
|
202
|
+
}
|
|
203
|
+
for (const [key, rerun] of this.waitingReruns) {
|
|
204
|
+
if (rerun.scope === scope)
|
|
205
|
+
this.waitingReruns.delete(key);
|
|
206
|
+
}
|
|
207
|
+
// A running run still uses the variables; `finish` frees them.
|
|
208
|
+
if (this.active?.scope !== scope)
|
|
209
|
+
this.worker?.postMessage({ type: 'dispose', scope: scope.id });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/** The worker every page's editors share. */
|
|
213
|
+
const pageRunner = new PythonRunner();
|
|
214
|
+
/** Runs that share Python variables, and the store their `pie.*` calls draw on. */
|
|
215
|
+
export class PythonScope {
|
|
216
|
+
store;
|
|
217
|
+
runner;
|
|
218
|
+
id = String(nextScopeId++);
|
|
219
|
+
disposed = false;
|
|
220
|
+
/**
|
|
221
|
+
* `store`: where `pie.*` calls draw. Without it they fail with "this page has no canvas".
|
|
222
|
+
* `runner`: the worker to run in. Default: the one every page shares.
|
|
223
|
+
*/
|
|
224
|
+
constructor(store, runner = pageRunner) {
|
|
225
|
+
this.store = store;
|
|
226
|
+
this.runner = runner;
|
|
227
|
+
}
|
|
228
|
+
/** Queues `code`. Returns a stop function. `error` comes only with `finished`. */
|
|
229
|
+
run(code, onStatus, onOutput, onPlot, onUi) {
|
|
230
|
+
return this.runner.enqueue({ scope: this, code, onStatus, onOutput, onPlot, onUi });
|
|
231
|
+
}
|
|
232
|
+
/** Queues a rerun of node `nodeId`'s `pie.lift`ed function. It waits while a block runs. */
|
|
233
|
+
rerun(nodeId, values, done) {
|
|
234
|
+
if (this.disposed)
|
|
235
|
+
return;
|
|
236
|
+
this.runner.rerun({ scope: this, nodeId, values, done });
|
|
237
|
+
}
|
|
238
|
+
/** Frees the variables. Call when the page goes away. */
|
|
239
|
+
dispose() {
|
|
240
|
+
this.disposed = true;
|
|
241
|
+
this.runner.dispose(this);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import PIE_PY from './py/pie.py?raw';
|
|
2
|
+
import PIE_MPL_PY from './py/pie_mpl.py?raw';
|
|
3
|
+
import SHOW_FIGURES from './py/show_figures.py?raw';
|
|
4
|
+
const BASE = 'https://cdn.jsdelivr.net/pyodide/v0.28.2/full/';
|
|
5
|
+
const pending = new Map();
|
|
6
|
+
let next = 0;
|
|
7
|
+
/** Posts `message` to the page and waits for its reply. */
|
|
8
|
+
const request = (message) => new Promise((resolve, reject) => {
|
|
9
|
+
const reqId = next++;
|
|
10
|
+
pending.set(reqId, { resolve, reject });
|
|
11
|
+
postMessage({ ...message, reqId });
|
|
12
|
+
});
|
|
13
|
+
const call = (keyword, args, options) => request({ type: 'call', keyword, args, options });
|
|
14
|
+
/** The node at `position` of an array's items. Creates nothing. */
|
|
15
|
+
const item = (arrayId, position) => request({ type: 'item', arrayId, position });
|
|
16
|
+
/** The node with id `id`. Creates nothing. */
|
|
17
|
+
const get_node = (id) => request({ type: 'get_node', id });
|
|
18
|
+
/** First half of `g(a)` for `g = pie.lift(f, ...)`: the input values and the output id. */
|
|
19
|
+
const lift = (name, inputIds, kind, out, fill, stroke) => request({ type: 'lift', name, inputIds, kind, out, fill, stroke });
|
|
20
|
+
/** Second half of `g(a)`: the first result. Python doesn't wait for a reply. */
|
|
21
|
+
const lift_save = (save) => postMessage({ type: 'lift_save', ...save });
|
|
22
|
+
/** `pie.ui.<name>(...)`: shows a UI below the block. `args`: nodes as `{id}`, or lists of them. */
|
|
23
|
+
const ui = (name, args, options) => request({ type: 'ui', name, args, options });
|
|
24
|
+
/** Results of a batch the page asked for in a reply, before an animation. Waits for the call's next reply. */
|
|
25
|
+
const batch_done = (results) => request({ type: 'batch_done', results });
|
|
26
|
+
const ready = (async () => {
|
|
27
|
+
const { loadPyodide } = await import(/* @vite-ignore */ `${BASE}pyodide.mjs`);
|
|
28
|
+
const py = await loadPyodide({ indexURL: BASE, env: { MPLBACKEND: 'module://pie_mpl' } });
|
|
29
|
+
py.setStdout({ batched: (text) => postMessage({ type: 'output', text: text + '\n' }) });
|
|
30
|
+
py.setStderr({ batched: (text) => postMessage({ type: 'output', text: text + '\n' }) });
|
|
31
|
+
py.registerJsModule('bridge', {
|
|
32
|
+
call, item, get_node, lift, lift_save, batch_done, ui, plot: (png) => postMessage({ type: 'plot', png }),
|
|
33
|
+
});
|
|
34
|
+
py.FS.writeFile('/home/pyodide/pie.py', PIE_PY);
|
|
35
|
+
py.FS.writeFile('/home/pyodide/pie_mpl.py', PIE_MPL_PY);
|
|
36
|
+
postMessage({ type: 'loaded' });
|
|
37
|
+
return py;
|
|
38
|
+
})();
|
|
39
|
+
// Scope id -> the Python globals its runs share. One scope per page.
|
|
40
|
+
const scopes = new Map();
|
|
41
|
+
const message = (e) => (e instanceof Error ? e.message : String(e));
|
|
42
|
+
self.onmessage = async ({ data }) => {
|
|
43
|
+
if (data.type === 'reply') {
|
|
44
|
+
const { resolve, reject } = pending.get(data.reqId);
|
|
45
|
+
pending.delete(data.reqId);
|
|
46
|
+
if (data.error !== undefined)
|
|
47
|
+
reject(new Error(data.error));
|
|
48
|
+
else
|
|
49
|
+
resolve(data.result);
|
|
50
|
+
}
|
|
51
|
+
if (data.type === 'run') {
|
|
52
|
+
let error;
|
|
53
|
+
try {
|
|
54
|
+
const py = await ready;
|
|
55
|
+
let globals = scopes.get(data.scope);
|
|
56
|
+
if (!globals) {
|
|
57
|
+
globals = py.globals.get('dict')();
|
|
58
|
+
py.runPython('import pie', { globals });
|
|
59
|
+
scopes.set(data.scope, globals);
|
|
60
|
+
}
|
|
61
|
+
// A batch finds lifted functions in the running block's globals.
|
|
62
|
+
const pie = py.pyimport('pie');
|
|
63
|
+
pie._scope = globals;
|
|
64
|
+
pie.destroy();
|
|
65
|
+
// Downloads packages the code imports (numpy, scipy, matplotlib, ...) on first use.
|
|
66
|
+
await py.loadPackagesFromImports(data.code);
|
|
67
|
+
try {
|
|
68
|
+
await py.runPythonAsync(data.code, { globals }); // Async entry is required for run_sync
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
error = message(e);
|
|
72
|
+
}
|
|
73
|
+
// Runs after an error too, so figures drawn before it still show.
|
|
74
|
+
py.runPython(SHOW_FIGURES);
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
error ??= message(e);
|
|
78
|
+
}
|
|
79
|
+
postMessage({ type: 'done', error });
|
|
80
|
+
}
|
|
81
|
+
// The page sends reruns only between runs, so this never interleaves with a run.
|
|
82
|
+
if (data.type === 'rerun') {
|
|
83
|
+
let result;
|
|
84
|
+
let error;
|
|
85
|
+
try {
|
|
86
|
+
const py = await ready;
|
|
87
|
+
const globals = scopes.get(data.scope);
|
|
88
|
+
if (!globals)
|
|
89
|
+
throw new Error("Python was restarted, so this node can't rerun. Run the block that made it again.");
|
|
90
|
+
const pie = py.pyimport('pie');
|
|
91
|
+
const values = py.toPy(data.values);
|
|
92
|
+
try {
|
|
93
|
+
const out = pie._rerun(globals, data.nodeId, values);
|
|
94
|
+
result = out.toJs({ dict_converter: Object.fromEntries });
|
|
95
|
+
out.destroy();
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
values.destroy();
|
|
99
|
+
pie.destroy();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
error = message(e);
|
|
104
|
+
}
|
|
105
|
+
postMessage({ type: 'rerun_done', reqId: data.reqId, result, error });
|
|
106
|
+
}
|
|
107
|
+
if (data.type === 'dispose') {
|
|
108
|
+
scopes.get(data.scope)?.destroy();
|
|
109
|
+
scopes.delete(data.scope);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { onCleanup, onMount } from 'solid-js';
|
|
2
|
+
import { clampToLayer, UI_LAYER_CLASS } from './dragUi';
|
|
3
|
+
/** A layer over the canvas for UIs dropped there. When the canvas resizes, they are kept inside it. */
|
|
4
|
+
export function UiLayer() {
|
|
5
|
+
let layer;
|
|
6
|
+
onMount(() => {
|
|
7
|
+
const observer = new ResizeObserver(() => {
|
|
8
|
+
for (const box of layer.children)
|
|
9
|
+
clampToLayer(box, layer);
|
|
10
|
+
});
|
|
11
|
+
observer.observe(layer);
|
|
12
|
+
onCleanup(() => observer.disconnect());
|
|
13
|
+
});
|
|
14
|
+
return <div ref={layer} class={UI_LAYER_CLASS}/>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NodeStore } from '../nodes/state/NodeStore';
|
|
2
|
+
/** A node as Python sends it, or a list of them at any depth. */
|
|
3
|
+
type SentArg = {
|
|
4
|
+
id: string;
|
|
5
|
+
} | SentArg[];
|
|
6
|
+
/** A `pie.ui.<name>(*args, **options)` call. */
|
|
7
|
+
export interface UiCall {
|
|
8
|
+
name: string;
|
|
9
|
+
args: SentArg[];
|
|
10
|
+
options: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** Throws, for Python to raise, when there is no such UI or a node is missing. */
|
|
13
|
+
export declare function checkUi(store: NodeStore, call: UiCall): void;
|
|
14
|
+
/**
|
|
15
|
+
* Shows a UI, with a grip to drag it into the page's text or onto the canvas. Shows nothing while one of its
|
|
16
|
+
* nodes is removed, and the error, after the UI's name, if it throws.
|
|
17
|
+
* The `label` option is shown in front of the UI, for every UI, so the UI itself doesn't get it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function UiView(props: {
|
|
20
|
+
store: NodeStore;
|
|
21
|
+
call: UiCall;
|
|
22
|
+
}): import("solid-js").JSX.Element;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ErrorBoundary, onCleanup, Show } from 'solid-js';
|
|
2
|
+
import { Dynamic } from 'solid-js/web';
|
|
3
|
+
import { getUi } from './registry';
|
|
4
|
+
import { nodeHandle, StoreContext } from './nodeHandle';
|
|
5
|
+
import { dragUi } from './dragUi';
|
|
6
|
+
const nodeIds = (args) => args.flatMap((a) => (Array.isArray(a) ? nodeIds(a) : [a.id]));
|
|
7
|
+
const toHandles = (store, args) => args.map((a) => (Array.isArray(a) ? toHandles(store, a) : nodeHandle(store, a.id)));
|
|
8
|
+
const present = (store, id) => {
|
|
9
|
+
const node = store.getNodeValue(id);
|
|
10
|
+
return node !== undefined && node.type !== 'Dummy';
|
|
11
|
+
};
|
|
12
|
+
/** Throws, for Python to raise, when there is no such UI or a node is missing. */
|
|
13
|
+
export function checkUi(store, call) {
|
|
14
|
+
if (!getUi(call.name))
|
|
15
|
+
throw new Error(`pie.ui: no UI named '${call.name}'`);
|
|
16
|
+
const missing = nodeIds(call.args).find((id) => !present(store, id));
|
|
17
|
+
if (missing !== undefined)
|
|
18
|
+
throw new Error(`pie.ui.${call.name}: no node with id '${missing}'`);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Shows a UI, with a grip to drag it into the page's text or onto the canvas. Shows nothing while one of its
|
|
22
|
+
* nodes is removed, and the error, after the UI's name, if it throws.
|
|
23
|
+
* The `label` option is shown in front of the UI, for every UI, so the UI itself doesn't get it.
|
|
24
|
+
*/
|
|
25
|
+
export function UiView(props) {
|
|
26
|
+
const { name, args, options: { label, ...options } } = props.call;
|
|
27
|
+
const ids = nodeIds(args);
|
|
28
|
+
const handles = toHandles(props.store, args);
|
|
29
|
+
const shown = () => ids.every((id) => present(props.store, id));
|
|
30
|
+
// `box` starts in `home`. Dragging moves it out, so the editor's list, which holds `home`, is left alone.
|
|
31
|
+
let home;
|
|
32
|
+
let box;
|
|
33
|
+
onCleanup(() => box.remove());
|
|
34
|
+
const startDrag = (e) => {
|
|
35
|
+
const text = home.closest('main');
|
|
36
|
+
if (text)
|
|
37
|
+
dragUi(e, box, text);
|
|
38
|
+
};
|
|
39
|
+
return (<div ref={home}>
|
|
40
|
+
<div ref={box} class="python-editor-ui" classList={{ 'python-editor-ui-hidden': !shown() }}>
|
|
41
|
+
<span class="python-editor-ui-grip" title="Drag to move" onPointerDown={startDrag}>⋮⋮</span>
|
|
42
|
+
<Show when={label != null && label !== ''}>
|
|
43
|
+
<span class="python-editor-ui-label">{String(label)}</span>
|
|
44
|
+
</Show>
|
|
45
|
+
<div class="python-editor-ui-body">
|
|
46
|
+
<StoreContext.Provider value={props.store}>
|
|
47
|
+
<ErrorBoundary fallback={(err) => <span class="python-editor-error">pie.ui.{name}: {err instanceof Error ? err.message : String(err)}</span>}>
|
|
48
|
+
<Show when={shown()}>
|
|
49
|
+
<Dynamic component={getUi(name)} {...options} args={handles}/>
|
|
50
|
+
</Show>
|
|
51
|
+
</ErrorBoundary>
|
|
52
|
+
</StoreContext.Provider>
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
</div>);
|
|
56
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type UiArg } from '../nodeHandle';
|
|
2
|
+
/**
|
|
3
|
+
* `pie.ui.checkboxes(mask, options=['a', 'b'])`: one checkbox per item of the Array of Scalars `mask`.
|
|
4
|
+
* An item is 1 when ticked, 0 when not; any value other than 0 shows as ticked.
|
|
5
|
+
* `options`: one label per item, in row-major order. Without it, the labels are the item positions.
|
|
6
|
+
*/
|
|
7
|
+
export declare function Checkboxes(props: {
|
|
8
|
+
args: UiArg[];
|
|
9
|
+
options?: string[];
|
|
10
|
+
}): import("solid-js").JSX.Element;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Index } from 'solid-js';
|
|
2
|
+
import { num, scalar } from '../../nodes/jax';
|
|
3
|
+
import { expectNode, useSetNode } from '../nodeHandle';
|
|
4
|
+
/**
|
|
5
|
+
* `pie.ui.checkboxes(mask, options=['a', 'b'])`: one checkbox per item of the Array of Scalars `mask`.
|
|
6
|
+
* An item is 1 when ticked, 0 when not; any value other than 0 shows as ticked.
|
|
7
|
+
* `options`: one label per item, in row-major order. Without it, the labels are the item positions.
|
|
8
|
+
*/
|
|
9
|
+
export function Checkboxes(props) {
|
|
10
|
+
const [mask] = expectNode(props.args[0], ['Array']);
|
|
11
|
+
const setNode = useSetNode();
|
|
12
|
+
const items = () => {
|
|
13
|
+
const array = mask();
|
|
14
|
+
if (array.itemType !== 'Scalar')
|
|
15
|
+
throw new Error(`expects an Array of Scalars, got an Array of ${array.itemType}`);
|
|
16
|
+
if (props.options && props.options.length !== array.items.length) {
|
|
17
|
+
throw new Error(`got ${props.options.length} options for ${array.items.length} items`);
|
|
18
|
+
}
|
|
19
|
+
return array.items;
|
|
20
|
+
};
|
|
21
|
+
// `Index`, not `For`: the items are new objects after every change, but their positions stay.
|
|
22
|
+
return (<span class="pie-ui-choices">
|
|
23
|
+
<Index each={items()}>
|
|
24
|
+
{(item, i) => (<label>
|
|
25
|
+
<input type="checkbox" checked={num(item().value) !== 0} onChange={(e) => setNode(item(), { value: scalar(e.currentTarget.checked ? 1 : 0) })}/>
|
|
26
|
+
{props.options?.[i] ?? String(i)}
|
|
27
|
+
</label>)}
|
|
28
|
+
</Index>
|
|
29
|
+
</span>);
|
|
30
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type UiArg } from '../nodeHandle';
|
|
2
|
+
/**
|
|
3
|
+
* `pie.ui.latex(a, b, ...)`: the nodes as LaTeX, side by side. A Scalar shows its number, a Point (x, y),
|
|
4
|
+
* a Text its value as LaTeX source, an Array of 1 or 2 dimensions a matrix.
|
|
5
|
+
*/
|
|
6
|
+
export declare function Latex(props: {
|
|
7
|
+
args: UiArg[];
|
|
8
|
+
}): import("solid-js").JSX.Element;
|