@tinyvolt/pie 0.2.0 → 0.3.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/init/site-agents.md +5 -2
- package/package.json +2 -1
- package/pages/python-editor.md +47 -16
- package/public/logo.svg +1 -1
- package/src/app.css +16 -0
- package/src/lib/markdown/render.ts +28 -0
- package/src/lib/nodes/animation.ts +47 -0
- package/src/lib/nodes/components/CenteredGrid.tsx +15 -2
- package/src/lib/nodes/components/NodeCanvas.tsx +7 -1
- package/src/lib/nodes/execute.ts +5 -2
- package/src/lib/nodes/functions/Node2NodeFunc.ts +3 -2
- package/src/lib/nodes/functions/sideEffects.ts +11 -22
- package/src/lib/nodes/state/NodeStore.ts +27 -1
- package/src/lib/python/lift.ts +87 -0
- package/src/lib/python/py/pie.py +24 -1
- package/src/lib/python/runner.ts +22 -3
- package/src/lib/python/worker.ts +8 -1
package/init/site-agents.md
CHANGED
|
@@ -94,7 +94,10 @@ Common mistake: `center = pie.point(1, 2)` does not give the node the id `center
|
|
|
94
94
|
- Every node the call makes gets the colors, including extra nodes such as the center point of `ellipse_from_foci`.
|
|
95
95
|
- A node shows only the colors its render function draws with. A point uses only `fill`, a line only `stroke`. For `partial`'s arrow, `stroke` colors the line and `fill` the head.
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
There are three system nodes:
|
|
98
|
+
- `unit`: a scalar holding the pixels per grid unit (default 50). Changing it changes the grid unit, e.g. `pie.scalar(80, out='unit')`.
|
|
99
|
+
- `gridOpacity`: a scalar that multiplies the opacity of the grid lines and axes (default 1; 0 hides them), e.g. `pie.scalar(0.5, out='gridOpacity')`.
|
|
100
|
+
- `canvasColor`: a text holding a CSS color for the canvas background (default `''`: no background), e.g. `pie.text('#111', out='canvasColor')`.
|
|
98
101
|
|
|
99
102
|
Built-in functions:
|
|
100
103
|
|
|
@@ -122,7 +125,7 @@ These change existing nodes instead of making a new one. They run once, when cal
|
|
|
122
125
|
|
|
123
126
|
| Call | Does |
|
|
124
127
|
| --- | --- |
|
|
125
|
-
| `pie.clear()` | removes every node and resets `unit` (pixels per grid unit) to 50 |
|
|
128
|
+
| `pie.clear()` | removes every node and resets the system nodes: `unit` (pixels per grid unit) to 50, `gridOpacity` to 1, `canvasColor` to `''` |
|
|
126
129
|
| `pie.translate_point_to(p, x, y)` | moves point `p` to (x, y) |
|
|
127
130
|
| `pie.translate_point_by(p, dx, dy)` | moves point `p` by (dx, dy) |
|
|
128
131
|
| `pie.translate_scalar_to(s, value)` | sets scalar `s` to `value` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinyvolt/pie",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A Markdown-based SSG for interactive science explainers. Create a site with: npx @tinyvolt/pie init my-site",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"@solidjs/router": "1.0.0",
|
|
41
41
|
"@solidjs/start": "2.0.6",
|
|
42
42
|
"codemirror": "6.0.2",
|
|
43
|
+
"@lezer/highlight": "1.2.5",
|
|
43
44
|
"katex": "0.19.0",
|
|
44
45
|
"marked": "18.1.1",
|
|
45
46
|
"nitro": "3.0.260610-beta"
|
package/pages/python-editor.md
CHANGED
|
@@ -1,29 +1,60 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
canvas: left
|
|
2
|
+
canvas: right
|
|
4
3
|
---
|
|
5
|
-
# Python editor
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
# Examples of animating lifted functions
|
|
6
|
+
|
|
7
|
+
1. One lifted function. The point should trace a sine curve, not jump to the end.
|
|
8
|
+
|
|
9
|
+
```python-editor
|
|
10
|
+
import math
|
|
11
|
+
def wave(t):
|
|
12
|
+
return (t, 2 * math.sin(t))
|
|
13
|
+
wave_point = pie.lift(wave, pie.Point)
|
|
14
|
+
t = pie.scalar(0, out='t')
|
|
15
|
+
wave_point(t)
|
|
16
|
+
pie.translate_scalar_to(t, 6, animate=True)
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
2. Change `t` after the animation. The point should jump to (1, 1.68) through a normal rerun.
|
|
14
20
|
|
|
15
|
-
|
|
21
|
+
```python-editor
|
|
22
|
+
pie.scalar(1, out='t')
|
|
23
|
+
```
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
3. Two lifted functions, one feeding the other. The point should slide from (0, 1) to (6, 1).
|
|
26
|
+
|
|
27
|
+
```python-editor
|
|
28
|
+
def double(x):
|
|
29
|
+
return 2 * x
|
|
30
|
+
def on_line(x):
|
|
31
|
+
return (x, 1)
|
|
32
|
+
double_l = pie.lift(double, pie.Scalar)
|
|
33
|
+
on_line_l = pie.lift(on_line, pie.Point)
|
|
34
|
+
s = pie.scalar(0, out='s')
|
|
35
|
+
on_line_l(double_l(s))
|
|
36
|
+
pie.translate_scalar_to(s, 3, animate=True)
|
|
21
37
|
```
|
|
22
38
|
|
|
39
|
+
4. A lifted function that returns an array. Five points should spread out smoothly from [0, 1] to [0, 5].
|
|
40
|
+
|
|
23
41
|
```python-editor
|
|
24
|
-
|
|
42
|
+
import numpy as np
|
|
43
|
+
linspace = pie.lift(np.linspace, pie.Array[pie.Scalar])
|
|
44
|
+
n = pie.scalar(1, out='n')
|
|
45
|
+
pie.point(linspace(0, n, 5), -2)
|
|
46
|
+
pie.translate_scalar_to(n, 5, animate=True)
|
|
25
47
|
```
|
|
26
48
|
|
|
49
|
+
5. A lifted function that fails partway. The point should move until x = 2 and then stop. The console should show one error: `fails_above_2: ValueError: too big`.
|
|
50
|
+
|
|
27
51
|
```python-editor
|
|
28
|
-
|
|
29
|
-
|
|
52
|
+
def fails_above_2(x):
|
|
53
|
+
if x > 2:
|
|
54
|
+
raise ValueError("too big")
|
|
55
|
+
return (x, -1)
|
|
56
|
+
fails_l = pie.lift(fails_above_2, pie.Point)
|
|
57
|
+
u = pie.scalar(0, out='u')
|
|
58
|
+
fails_l(u)
|
|
59
|
+
pie.translate_scalar_to(u, 4, animate=True)
|
|
60
|
+
```
|
package/public/logo.svg
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 21 64 45" width="256" height="180">
|
|
2
2
|
<path d="M32 29 L8 29 A24 24 0 0 0 20 49.78 Z" fill="#41DBC9"/>
|
|
3
3
|
<path d="M32 29 L44 49.78 A24 24 0 0 0 56 29 Z" fill="#41DBC9"/>
|
|
4
4
|
<path d="M32 34 L20 54.78 A24 24 0 0 0 44 54.78 Z" fill="#0B6B73"/>
|
package/src/app.css
CHANGED
|
@@ -128,6 +128,22 @@ main pre code {
|
|
|
128
128
|
background: none;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/* ```python tokens. Same colors as the editor's default theme. */
|
|
132
|
+
.tok-keyword { color: #708; }
|
|
133
|
+
.tok-atom, .tok-bool { color: #219; }
|
|
134
|
+
.tok-literal, .tok-number { color: #164; }
|
|
135
|
+
.tok-string { color: #a11; }
|
|
136
|
+
.tok-string2 { color: #e40; }
|
|
137
|
+
.tok-variableName.tok-definition { color: #00f; }
|
|
138
|
+
.tok-variableName.tok-local { color: #30a; }
|
|
139
|
+
.tok-typeName, .tok-namespace { color: #085; }
|
|
140
|
+
.tok-className { color: #167; }
|
|
141
|
+
.tok-variableName2, .tok-macroName { color: #256; }
|
|
142
|
+
.tok-propertyName.tok-definition { color: #00c; }
|
|
143
|
+
.tok-comment { color: #940; }
|
|
144
|
+
.tok-meta { color: #404740; }
|
|
145
|
+
.tok-invalid { color: #f00; }
|
|
146
|
+
|
|
131
147
|
main table {
|
|
132
148
|
border-collapse: collapse;
|
|
133
149
|
}
|
|
@@ -1,10 +1,33 @@
|
|
|
1
1
|
import katex from 'katex';
|
|
2
2
|
import { Marked, Renderer } from 'marked';
|
|
3
|
+
import { pythonLanguage } from '@codemirror/lang-python';
|
|
4
|
+
import { classHighlighter, highlightCode } from '@lezer/highlight';
|
|
3
5
|
|
|
4
6
|
function renderMath(formula: string, displayMode: boolean): string {
|
|
5
7
|
return katex.renderToString(formula, { displayMode, throwOnError: false });
|
|
6
8
|
}
|
|
7
9
|
|
|
10
|
+
function escapeHtml(text: string): string {
|
|
11
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Python as HTML, with `tok-*` classes on each token. app.css colors them like the editor. */
|
|
15
|
+
function highlightPython(code: string): string {
|
|
16
|
+
let html = '';
|
|
17
|
+
highlightCode(
|
|
18
|
+
code,
|
|
19
|
+
pythonLanguage.parser.parse(code),
|
|
20
|
+
classHighlighter,
|
|
21
|
+
(text, classes) => {
|
|
22
|
+
html += classes ? `<span class="${classes}">${escapeHtml(text)}</span>` : escapeHtml(text);
|
|
23
|
+
},
|
|
24
|
+
() => {
|
|
25
|
+
html += '\n';
|
|
26
|
+
},
|
|
27
|
+
);
|
|
28
|
+
return html;
|
|
29
|
+
}
|
|
30
|
+
|
|
8
31
|
const marked = new Marked({
|
|
9
32
|
extensions: [
|
|
10
33
|
{
|
|
@@ -49,8 +72,13 @@ const marked = new Marked({
|
|
|
49
72
|
],
|
|
50
73
|
renderer: {
|
|
51
74
|
// ```python-editor collapsed: marked keeps only the first word, so mark the block for the editor here.
|
|
75
|
+
// ```python: color each token here, at build time.
|
|
52
76
|
code(token) {
|
|
53
77
|
const [lang, ...flags] = (token.lang ?? '').split(/\s+/);
|
|
78
|
+
if (lang === 'python') {
|
|
79
|
+
const code = token.text.replace(/\n$/, '') + '\n';
|
|
80
|
+
return `<pre><code class="language-python">${highlightPython(code)}</code></pre>\n`;
|
|
81
|
+
}
|
|
54
82
|
if (lang !== 'python-editor' || !flags.includes('collapsed')) return false;
|
|
55
83
|
return new Renderer().code(token).replace('<code ', '<code data-collapsed ');
|
|
56
84
|
},
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { batch } from 'solid-js';
|
|
2
|
+
|
|
3
|
+
/** How long an animation takes. */
|
|
4
|
+
const ANIMATION_MS = 500;
|
|
5
|
+
|
|
6
|
+
/** The steps an animation plays: one per frame at 60 frames a second. Each frame shows the nearest step. */
|
|
7
|
+
const STEPS = 30;
|
|
8
|
+
|
|
9
|
+
/** Slow at the start and end, fast in the middle. `t` and the result go from 0 to 1. */
|
|
10
|
+
const easeInOut = (t: number) => (t < 0.5 ? 2 * t * t : 1 - 2 * (1 - t) ** 2);
|
|
11
|
+
|
|
12
|
+
/** One animated change, e.g. a point moving. */
|
|
13
|
+
export interface Track {
|
|
14
|
+
/** Shows the change at `t`, from 0 (start) to 1 (end). */
|
|
15
|
+
seek: (t: number) => void;
|
|
16
|
+
/** True once the node was replaced or removed: the track no longer changes anything. */
|
|
17
|
+
stopped: () => boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Runs before an animation plays, e.g. to get every value its steps need. `visitSteps` shows each step
|
|
22
|
+
* in turn, then goes back to the start. Can return a function to call when the animation ends.
|
|
23
|
+
*/
|
|
24
|
+
export type Prepare = (visitSteps: () => void) => Promise<(() => void) | void>;
|
|
25
|
+
|
|
26
|
+
/** Plays `tracks` together, after `prepare`. Resolves when they end, or once every track is stopped. */
|
|
27
|
+
export async function play(tracks: Track[], prepare?: Prepare): Promise<void> {
|
|
28
|
+
const seek = (step: number) => batch(() => {
|
|
29
|
+
const t = easeInOut(step / STEPS);
|
|
30
|
+
for (const track of tracks) track.seek(t);
|
|
31
|
+
});
|
|
32
|
+
const end = await prepare?.(() => {
|
|
33
|
+
for (let step = 0; step <= STEPS; step++) seek(step);
|
|
34
|
+
seek(0);
|
|
35
|
+
});
|
|
36
|
+
await new Promise<void>((resolve) => {
|
|
37
|
+
const start = performance.now();
|
|
38
|
+
requestAnimationFrame(function frame() {
|
|
39
|
+
if (tracks.every(track => track.stopped())) return resolve();
|
|
40
|
+
const step = Math.min(Math.round((performance.now() - start) / ANIMATION_MS * STEPS), STEPS);
|
|
41
|
+
seek(step);
|
|
42
|
+
if (step < STEPS) requestAnimationFrame(frame);
|
|
43
|
+
else resolve();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
end?.();
|
|
47
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JSX, createMemo } from 'solid-js';
|
|
1
|
+
import { JSX, createMemo, Show } from 'solid-js';
|
|
2
2
|
import { MIN_GRID_UNIT } from '../state/NodeStore';
|
|
3
3
|
|
|
4
4
|
interface CenteredGridProps {
|
|
@@ -7,6 +7,10 @@ interface CenteredGridProps {
|
|
|
7
7
|
height: number;
|
|
8
8
|
/** Pixels per grid unit. */
|
|
9
9
|
unit: number;
|
|
10
|
+
/** Opacity multiplier for the grid lines and axes (0 hides them). Defaults to 1. */
|
|
11
|
+
gridOpacity?: number;
|
|
12
|
+
/** Solid fill painted behind the grid. Defaults to transparent (no rect). */
|
|
13
|
+
backgroundColor?: string;
|
|
10
14
|
children: JSX.Element;
|
|
11
15
|
}
|
|
12
16
|
|
|
@@ -52,7 +56,16 @@ export function CenteredGrid(props: CenteredGridProps) {
|
|
|
52
56
|
|
|
53
57
|
return (
|
|
54
58
|
<g transform={`translate(${originX()}, ${originY()}) scale(1, -1)`}>
|
|
55
|
-
{
|
|
59
|
+
<Show when={props.backgroundColor}>
|
|
60
|
+
<rect
|
|
61
|
+
x={extents().xLeft}
|
|
62
|
+
y={extents().yBottom}
|
|
63
|
+
width={props.width}
|
|
64
|
+
height={props.height}
|
|
65
|
+
fill={props.backgroundColor}
|
|
66
|
+
/>
|
|
67
|
+
</Show>
|
|
68
|
+
<g opacity={props.gridOpacity ?? 1}>{gridLines()}</g>
|
|
56
69
|
{props.children}
|
|
57
70
|
</g>
|
|
58
71
|
);
|
|
@@ -46,7 +46,13 @@ export function NodeCanvas(props: NodeCanvasProps): JSX.Element {
|
|
|
46
46
|
return (
|
|
47
47
|
<div class="node-canvas" ref={svgContainerRef}>
|
|
48
48
|
<svg viewBox={`0 0 ${viewWidth()} ${viewHeight()}`}>
|
|
49
|
-
<CenteredGrid
|
|
49
|
+
<CenteredGrid
|
|
50
|
+
width={viewWidth()}
|
|
51
|
+
height={viewHeight()}
|
|
52
|
+
unit={props.store.unit()}
|
|
53
|
+
gridOpacity={props.store.gridOpacity()}
|
|
54
|
+
backgroundColor={props.store.canvasColor()}
|
|
55
|
+
>
|
|
50
56
|
{/* Step 1: Draw non-`Point` nodes first */}
|
|
51
57
|
<For each={layers().geometry}>{(nodeSignal) => nodeGroup(nodeSignal)}</For>
|
|
52
58
|
{/* Step 2: Draw `Point` nodes above geometry */}
|
package/src/lib/nodes/execute.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { AnyNode, ArrayNode, NodeType, ScalarNode, TextNode, checkColor } from '
|
|
|
6
6
|
import { scalar } from './jax';
|
|
7
7
|
import { leaves, listItemType, listShape, sha256 } from './tensors/tensor';
|
|
8
8
|
import { broadcastItems, broadcastShapes, isBroadcast } from './tensors/broadcast';
|
|
9
|
+
import { play, type Prepare, type Track } from './animation';
|
|
9
10
|
|
|
10
11
|
/** One argument of a Python call: a number, a string, a handle to an existing node, or a nested list of these. */
|
|
11
12
|
export type Arg = number | string | { id: string } | Arg[];
|
|
@@ -100,7 +101,8 @@ export interface CallOptions {
|
|
|
100
101
|
animate?: boolean;
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
|
|
104
|
+
/** `prepare`: runs before an animation the call starts plays. */
|
|
105
|
+
export async function execute(store: NodeStore, keyword: string, args: Arg[], { out, hidden, fill, stroke, animate }: CallOptions = {}, prepare?: Prepare): Promise<ExecuteResult> {
|
|
104
106
|
const func = getFunction(keyword) ?? getSideEffect(keyword);
|
|
105
107
|
if (!func) {
|
|
106
108
|
throw new Error(`Unknown function: ${keyword}`);
|
|
@@ -150,7 +152,8 @@ export async function execute(store: NodeStore, keyword: string, args: Arg[], {
|
|
|
150
152
|
|
|
151
153
|
if ('run' in func) {
|
|
152
154
|
const runs = batch(() => broadcastItems(func.parameters, inputs()).map(itemInputs => func.run(itemInputs, store, animate ?? false)));
|
|
153
|
-
|
|
155
|
+
const tracks = runs.filter((run): run is Track => run !== undefined);
|
|
156
|
+
if (tracks.length > 0) await play(tracks, prepare);
|
|
154
157
|
return { id: '', type: 'Dummy' };
|
|
155
158
|
}
|
|
156
159
|
|
|
@@ -3,6 +3,7 @@ import { batch, createComputed, createMemo, createRoot, onCleanup } from 'solid-
|
|
|
3
3
|
import { AnyNode } from '../state/Node';
|
|
4
4
|
import { NodeStore } from '../state/NodeStore';
|
|
5
5
|
import { computeBlueprint } from '../tensors/broadcast';
|
|
6
|
+
import type { Track } from '../animation';
|
|
6
7
|
|
|
7
8
|
export interface ParameterDefinition {
|
|
8
9
|
name: string;
|
|
@@ -44,8 +45,8 @@ export interface SideEffectFunc {
|
|
|
44
45
|
broadcastable?: boolean;
|
|
45
46
|
/** True: takes `animate`. Default false. */
|
|
46
47
|
animatable?: boolean;
|
|
47
|
-
/**
|
|
48
|
-
run: (inputs: AnyNode[], store: NodeStore, animate: boolean) => void |
|
|
48
|
+
/** An animated change returns its track. The call waits until it has played. */
|
|
49
|
+
run: (inputs: AnyNode[], store: NodeStore, animate: boolean) => void | Track;
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
type LocalMeta = {
|
|
@@ -3,46 +3,35 @@ import { SideEffectFunc } from './Node2NodeFunc';
|
|
|
3
3
|
import { AnyNode, PointNode, ScalarNode, TextNode, checkColor, getNodeTypes } from '../state/Node';
|
|
4
4
|
import { NodeStore } from '../state/NodeStore';
|
|
5
5
|
import { Dtype, add, num, ref, scalar } from '../jax';
|
|
6
|
-
|
|
7
|
-
/** How long an animated change takes. */
|
|
8
|
-
const ANIMATION_MS = 500;
|
|
9
|
-
|
|
10
|
-
/** Slow at the start and end, fast in the middle. `t` and the result go from 0 to 1. */
|
|
11
|
-
const easeInOut = (t: number) => (t < 0.5 ? 2 * t * t : 1 - 2 * (1 - t) ** 2);
|
|
6
|
+
import type { Track } from '../animation';
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
9
|
* Saves `node` with `changes`, new values for some of its numbers, in place of `node`.
|
|
15
10
|
* It has no inputs, so it no longer follows the old node's.
|
|
16
|
-
* With `animate`, the numbers
|
|
17
|
-
* Resolves when they arrive, or when the node is replaced or removed first.
|
|
11
|
+
* With `animate`, returns a track that moves the numbers from their old values to the new ones.
|
|
18
12
|
*/
|
|
19
|
-
function replaceNode<N extends AnyNode>(store: NodeStore, node: N, changes: Partial<N>, animate: boolean):
|
|
13
|
+
function replaceNode<N extends AnyNode>(store: NodeStore, node: N, changes: Partial<N>, animate: boolean): Track | undefined {
|
|
20
14
|
const { __graph_deps, ...unchanged } = node;
|
|
21
15
|
const end = { ...unchanged, ...changes } as AnyNode;
|
|
22
16
|
if (!animate) {
|
|
23
17
|
createRoot((dispose) => store.saveNode(node.id, createMemo(() => end), dispose));
|
|
24
|
-
return
|
|
18
|
+
return;
|
|
25
19
|
}
|
|
26
20
|
|
|
27
21
|
// [key, start, end] for each changed number.
|
|
28
22
|
const moves = Object.entries(changes).map(([key, value]) => [key, num(node[key as keyof N] as Dtype), num(value)] as const);
|
|
29
23
|
const at = (t: number) => ({ ...end, ...Object.fromEntries(moves.map(([key, a, b]) => [key, scalar(a + (b - a) * t)])) } as AnyNode);
|
|
30
24
|
|
|
31
|
-
return
|
|
32
|
-
const start = performance.now();
|
|
25
|
+
return createRoot((dispose) => {
|
|
33
26
|
const [progress, setProgress] = createSignal(0);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (progress() < 1) frame = requestAnimationFrame(step);
|
|
38
|
-
else resolve();
|
|
39
|
-
});
|
|
40
|
-
// Another change to this node, or `clear`, disposes this one: stop moving and let the call return.
|
|
27
|
+
let stopped = false;
|
|
28
|
+
store.saveNode(node.id, createMemo(() => (progress() < 1 ? at(progress()) : end)), dispose);
|
|
29
|
+
// Another change to this node, or `clear`, disposes this one: it stops moving.
|
|
41
30
|
onCleanup(() => {
|
|
42
|
-
|
|
43
|
-
resolve();
|
|
31
|
+
stopped = true;
|
|
44
32
|
});
|
|
45
|
-
|
|
33
|
+
return { seek: setProgress, stopped: () => stopped };
|
|
34
|
+
});
|
|
46
35
|
}
|
|
47
36
|
|
|
48
37
|
// Any node, but not a whole Array: an Array is looped over, so each item is hidden or shown.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Accessor, batch, createMemo, createRoot, createSignal, untrack } from 'solid-js';
|
|
2
2
|
import { createStore, reconcile } from 'solid-js/store';
|
|
3
|
-
import { AnyNode, ScalarNode } from './Node';
|
|
3
|
+
import { AnyNode, ScalarNode, TextNode } from './Node';
|
|
4
4
|
import { NameGenerator } from './NameGenerator';
|
|
5
5
|
import { num, scalar } from '../jax';
|
|
6
6
|
|
|
@@ -22,6 +22,10 @@ export class NodeStore {
|
|
|
22
22
|
|
|
23
23
|
/** Pixels per grid unit, read from the `unit` node. */
|
|
24
24
|
public unit!: Accessor<number>;
|
|
25
|
+
/** Opacity multiplier for the grid lines and axes, read from the `gridOpacity` node. */
|
|
26
|
+
public gridOpacity!: Accessor<number>;
|
|
27
|
+
/** Canvas background color, read from the `canvasColor` node. `undefined` when empty: no background. */
|
|
28
|
+
public canvasColor!: Accessor<string | undefined>;
|
|
25
29
|
|
|
26
30
|
public nameGenerator: NameGenerator;
|
|
27
31
|
|
|
@@ -41,6 +45,16 @@ export class NodeStore {
|
|
|
41
45
|
// max(1, ...): a zero, negative or NaN unit would break the viewBox.
|
|
42
46
|
return node ? Math.max(1, num(node.value)) : 50;
|
|
43
47
|
});
|
|
48
|
+
|
|
49
|
+
this.gridOpacity = createMemo(() => {
|
|
50
|
+
const node = this.nodes['gridOpacity']?.signal() as ScalarNode | undefined;
|
|
51
|
+
return node ? num(node.value) : 1;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
this.canvasColor = createMemo(() => {
|
|
55
|
+
const node = this.nodes['canvasColor']?.signal() as TextNode | undefined;
|
|
56
|
+
return node?.value?.trim() || undefined;
|
|
57
|
+
});
|
|
44
58
|
});
|
|
45
59
|
|
|
46
60
|
// untrack: names are generated inside reactive computations, which must not rerun when the checked node changes.
|
|
@@ -55,6 +69,18 @@ export class NodeStore {
|
|
|
55
69
|
const signal = createMemo(() => ({ id: unitId, type: 'Scalar', value: scalar(50) } as ScalarNode));
|
|
56
70
|
this.saveNode(unitId, signal, dispose);
|
|
57
71
|
});
|
|
72
|
+
|
|
73
|
+
const gridOpacityId = 'gridOpacity';
|
|
74
|
+
createRoot((dispose) => {
|
|
75
|
+
const signal = createMemo(() => ({ id: gridOpacityId, type: 'Scalar', value: scalar(1) } as ScalarNode));
|
|
76
|
+
this.saveNode(gridOpacityId, signal, dispose);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const canvasColorId = 'canvasColor';
|
|
80
|
+
createRoot((dispose) => {
|
|
81
|
+
const signal = createMemo(() => ({ id: canvasColorId, type: 'Text', value: '' } as TextNode));
|
|
82
|
+
this.saveNode(canvasColorId, signal, dispose);
|
|
83
|
+
});
|
|
58
84
|
}
|
|
59
85
|
|
|
60
86
|
public clearNodes() {
|
package/src/lib/python/lift.ts
CHANGED
|
@@ -32,6 +32,29 @@ export interface LiftSave extends PyResult {
|
|
|
32
32
|
|
|
33
33
|
export type Rerun = (nodeId: string, values: InputValue[], done: (result?: PyResult, error?: string) => void) => void;
|
|
34
34
|
|
|
35
|
+
/** Before an animation: run node `nodeId`'s lifted function once per entry of `inputs`, one per step. */
|
|
36
|
+
export interface BatchJob {
|
|
37
|
+
nodeId: string;
|
|
38
|
+
inputs: InputValue[][];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The function's result for one entry of a job's `inputs`, or the error it raised. */
|
|
42
|
+
export type BatchResult = PyResult | { error: string };
|
|
43
|
+
|
|
44
|
+
type Blueprint = Record<string, BlueprintNode>;
|
|
45
|
+
|
|
46
|
+
/** A lifted node's part in an animation. */
|
|
47
|
+
interface Animated {
|
|
48
|
+
nodeId: string;
|
|
49
|
+
/** Saves the results for inputs the steps need, by input values. */
|
|
50
|
+
save: (results: Map<string, BatchResult>) => void;
|
|
51
|
+
/** Ends the animation: the node keeps the last step's result, as if a rerun had returned it. */
|
|
52
|
+
end: () => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Set while an animation's steps are visited: the inputs each lifted node has no result for, by input values.
|
|
56
|
+
let missing: Map<Animated, Map<string, InputValue[]>> | undefined;
|
|
57
|
+
|
|
35
58
|
function inputValue(node: AnyNode | undefined, id: string): InputValue {
|
|
36
59
|
if (!node) throw new Error(`Unknown node '${id}'`);
|
|
37
60
|
if (node.type === 'Scalar') return num(node.value);
|
|
@@ -71,15 +94,67 @@ function toBlueprint(kind: Kind, itemType: ItemType | undefined, result: PyResul
|
|
|
71
94
|
return blueprint;
|
|
72
95
|
}
|
|
73
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Before an animation: gets every lifted result its steps need from `runBatch`, one trip for all of them.
|
|
99
|
+
* `visitSteps` shows each step, and the lifted nodes note the inputs they have no result for. A result
|
|
100
|
+
* can change the inputs of a lifted node that depends on it, so this repeats until nothing is missing.
|
|
101
|
+
* Returns the function that ends the animation for these nodes. Errors go to the console.
|
|
102
|
+
*/
|
|
103
|
+
export async function prepareLifts(visitSteps: () => void, runBatch: (jobs: BatchJob[]) => Promise<BatchResult[][]>) {
|
|
104
|
+
const animated = new Set<Animated>();
|
|
105
|
+
for (;;) {
|
|
106
|
+
const found = new Map<Animated, Map<string, InputValue[]>>();
|
|
107
|
+
missing = found;
|
|
108
|
+
try {
|
|
109
|
+
visitSteps();
|
|
110
|
+
} finally {
|
|
111
|
+
missing = undefined;
|
|
112
|
+
}
|
|
113
|
+
if (found.size === 0) return () => animated.forEach(node => node.end());
|
|
114
|
+
const nodes = [...found.keys()];
|
|
115
|
+
const results = await runBatch(nodes.map(node => ({ nodeId: node.nodeId, inputs: [...found.get(node)!.values()] })));
|
|
116
|
+
nodes.forEach((node, i) => {
|
|
117
|
+
const keys = [...found.get(node)!.keys()];
|
|
118
|
+
node.save(new Map(keys.map((key, j) => [key, results[i][j]])));
|
|
119
|
+
animated.add(node);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
74
124
|
/**
|
|
75
125
|
* Second half of `g(a)`: saves the node with Python's first result. When an input's value changes,
|
|
76
126
|
* the node keeps its value and asks `rerun` for a new one. Rerun errors go to the console.
|
|
127
|
+
* During an animation it shows the results `prepareLifts` got for the steps instead.
|
|
77
128
|
*/
|
|
78
129
|
export function saveLift(store: NodeStore, save: LiftSave, rerun: Rerun) {
|
|
79
130
|
const { id, name, inputIds, kind, itemType, hidden, fill, stroke } = save;
|
|
80
131
|
const [blueprint, setBlueprint] = createSignal(toBlueprint(kind, itemType, save));
|
|
81
132
|
// The input values of the latest result or rerun request. Unset until the first compute, which uses the first result.
|
|
82
133
|
let requested: string | undefined;
|
|
134
|
+
// During an animation: the results for its steps, by input values. `null`: the function failed for them.
|
|
135
|
+
const steps = new Map<string, Blueprint | null>();
|
|
136
|
+
// The input values of the step shown last.
|
|
137
|
+
let shownStep: string | undefined;
|
|
138
|
+
|
|
139
|
+
const animated: Animated = {
|
|
140
|
+
nodeId: id,
|
|
141
|
+
save(results) {
|
|
142
|
+
let error: string | undefined;
|
|
143
|
+
for (const [key, result] of results) {
|
|
144
|
+
if ('error' in result) error ??= result.error;
|
|
145
|
+
steps.set(key, 'error' in result ? null : toBlueprint(kind, itemType, result));
|
|
146
|
+
}
|
|
147
|
+
if (error !== undefined) console.error(`${name}: ${error}`);
|
|
148
|
+
},
|
|
149
|
+
end() {
|
|
150
|
+
if (shownStep === undefined) return;
|
|
151
|
+
const shown = steps.get(shownStep);
|
|
152
|
+
requested = shownStep;
|
|
153
|
+
shownStep = undefined;
|
|
154
|
+
steps.clear();
|
|
155
|
+
if (shown) setBlueprint(shown);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
83
158
|
|
|
84
159
|
const func: Node2NodeFunc = {
|
|
85
160
|
keyword: `${PYTHON_KEYWORD_PREFIX}${name}`,
|
|
@@ -90,6 +165,12 @@ export function saveLift(store: NodeStore, save: LiftSave, rerun: Rerun) {
|
|
|
90
165
|
compute(inputs) {
|
|
91
166
|
const values = inputs.map((node, i) => inputValue(node, inputIds[i]));
|
|
92
167
|
const key = JSON.stringify(values);
|
|
168
|
+
if (missing || steps.has(key)) {
|
|
169
|
+
const step = steps.get(key);
|
|
170
|
+
if (step === undefined) noteMissing(animated, key, values);
|
|
171
|
+
else shownStep = key;
|
|
172
|
+
return step ?? blueprint();
|
|
173
|
+
}
|
|
93
174
|
if (requested !== undefined && key !== requested) {
|
|
94
175
|
rerun(id, values, (result, error) => {
|
|
95
176
|
if (error !== undefined) console.error(`${name}: ${error}`);
|
|
@@ -103,3 +184,9 @@ export function saveLift(store: NodeStore, save: LiftSave, rerun: Rerun) {
|
|
|
103
184
|
};
|
|
104
185
|
new NodeReconciler().evaluate(func, inputIds, store, id, hidden, fill, stroke);
|
|
105
186
|
}
|
|
187
|
+
|
|
188
|
+
function noteMissing(node: Animated, key: string, values: InputValue[]) {
|
|
189
|
+
let inputs = missing!.get(node);
|
|
190
|
+
if (!inputs) missing!.set(node, inputs = new Map());
|
|
191
|
+
inputs.set(key, values);
|
|
192
|
+
}
|
package/src/lib/python/py/pie.py
CHANGED
|
@@ -86,7 +86,7 @@ def _call(name, args, **options):
|
|
|
86
86
|
_check_bridge()
|
|
87
87
|
js_args = to_js([_arg(a) for a in args], dict_converter=Object.fromEntries)
|
|
88
88
|
js_options = to_js({k: v for k, v in options.items() if v is not None}, dict_converter=Object.fromEntries)
|
|
89
|
-
return _node(
|
|
89
|
+
return _node(_wait(bridge.call(name, js_args, js_options)))
|
|
90
90
|
|
|
91
91
|
# Above 0 while a function passed to `pie.lift` runs. It can't reach the page: a rerun runs outside any block.
|
|
92
92
|
_lift_depth = 0
|
|
@@ -162,6 +162,29 @@ def _rerun(scope, node_id, values):
|
|
|
162
162
|
shape, values = _evaluate(scope, call, values)
|
|
163
163
|
return {"shape": shape, "values": values}
|
|
164
164
|
|
|
165
|
+
# The running block's globals. The worker sets it before each run, so a batch can find lifted functions.
|
|
166
|
+
_scope = None
|
|
167
|
+
|
|
168
|
+
def _wait(request):
|
|
169
|
+
"""
|
|
170
|
+
Waits for the page's reply to `request`. Before an animation, the reply can be a batch instead: the lifted
|
|
171
|
+
functions to run for its steps. Then runs them, sends the results, and waits for the next reply.
|
|
172
|
+
"""
|
|
173
|
+
reply = run_sync(request)
|
|
174
|
+
while (batch := getattr(reply, "batch", None)) is not None:
|
|
175
|
+
reply = run_sync(bridge.batch_done(to_js(_run_batch(batch), dict_converter=Object.fromEntries)))
|
|
176
|
+
return reply
|
|
177
|
+
|
|
178
|
+
def _run_batch(batch):
|
|
179
|
+
"""Each job's lifted function, once per entry of its inputs. A failure gives {"error": message} for that entry."""
|
|
180
|
+
return [[_try_rerun(job.nodeId, values) for values in job.inputs.to_py()] for job in batch]
|
|
181
|
+
|
|
182
|
+
def _try_rerun(node_id, values):
|
|
183
|
+
try:
|
|
184
|
+
return _rerun(_scope, node_id, values)
|
|
185
|
+
except Exception as e:
|
|
186
|
+
return {"error": f"{type(e).__name__}: {e}"}
|
|
187
|
+
|
|
165
188
|
def _evaluate(scope, call, values):
|
|
166
189
|
"""Calls the function: by name, the one now named so in `scope`; otherwise the one kept."""
|
|
167
190
|
global _lift_depth
|
package/src/lib/python/runner.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { NodeStore } from '~/lib/nodes/state/NodeStore';
|
|
2
2
|
import { execute } from '~/lib/nodes/execute';
|
|
3
3
|
import { getItem } from '~/lib/nodes/tensors/tensor';
|
|
4
|
-
import { saveLift, startLift, type InputValue, type PyResult } from './lift';
|
|
4
|
+
import { prepareLifts, saveLift, startLift, type BatchJob, type BatchResult, type InputValue, type PyResult } from './lift';
|
|
5
5
|
|
|
6
6
|
export type RunStatus = 'waiting' | 'loading' | 'running' | 'finished' | 'stopped' | 'killed';
|
|
7
7
|
|
|
@@ -34,6 +34,9 @@ const waitingReruns = new Map<string, Rerun>();
|
|
|
34
34
|
const sentReruns = new Map<number, Rerun>();
|
|
35
35
|
let nextRerunId = 0;
|
|
36
36
|
|
|
37
|
+
// Set while a `pie.*` call waits for Python to run a batch: takes Python's results and the id of its request.
|
|
38
|
+
let batchDone: ((reqId: number, results: BatchResult[][]) => void) | undefined;
|
|
39
|
+
|
|
37
40
|
const message = (err: unknown) => (err instanceof Error ? err.message : String(err));
|
|
38
41
|
|
|
39
42
|
function onMessage(this: Worker, { data }: MessageEvent) {
|
|
@@ -43,9 +46,11 @@ function onMessage(this: Worker, { data }: MessageEvent) {
|
|
|
43
46
|
run?.onStatus('running');
|
|
44
47
|
}
|
|
45
48
|
if (data.type === 'call' || data.type === 'item' || data.type === 'lift') {
|
|
49
|
+
// The request to answer: this one, or, after a batch, Python's request with the batch's results.
|
|
50
|
+
let reqId: number = data.reqId;
|
|
46
51
|
// A reply from before a kill must not reach the new worker.
|
|
47
52
|
const reply = (msg: { result?: unknown; error?: string }) => {
|
|
48
|
-
if (worker === this) this.postMessage({ type: 'reply', reqId
|
|
53
|
+
if (worker === this) this.postMessage({ type: 'reply', reqId, ...msg });
|
|
49
54
|
};
|
|
50
55
|
const store = run?.scope.store;
|
|
51
56
|
if (!store) {
|
|
@@ -68,11 +73,24 @@ function onMessage(this: Worker, { data }: MessageEvent) {
|
|
|
68
73
|
}
|
|
69
74
|
return;
|
|
70
75
|
}
|
|
71
|
-
|
|
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: BatchJob[]) => new Promise<BatchResult[][]>((resolve) => {
|
|
78
|
+
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(
|
|
72
85
|
(result) => reply({ result }),
|
|
73
86
|
(err) => reply({ error: message(err) }),
|
|
74
87
|
);
|
|
75
88
|
}
|
|
89
|
+
if (data.type === 'batch_done') {
|
|
90
|
+
const done = batchDone;
|
|
91
|
+
batchDone = undefined;
|
|
92
|
+
done?.(data.reqId, data.results);
|
|
93
|
+
}
|
|
76
94
|
// No reply: Python doesn't wait for it.
|
|
77
95
|
if (data.type === 'lift_save') {
|
|
78
96
|
const scope = run?.scope;
|
|
@@ -142,6 +160,7 @@ function killWorker() {
|
|
|
142
160
|
loaded = false;
|
|
143
161
|
// Their replies will never come.
|
|
144
162
|
sentReruns.clear();
|
|
163
|
+
batchDone = undefined;
|
|
145
164
|
}
|
|
146
165
|
|
|
147
166
|
/** Kills Python. The running run and every queued run end as killed. */
|
package/src/lib/python/worker.ts
CHANGED
|
@@ -29,13 +29,16 @@ const lift = (name: string, inputIds: string[], kind: string, out?: string, fill
|
|
|
29
29
|
/** Second half of `g(a)`: the first result. Python doesn't wait for a reply. */
|
|
30
30
|
const lift_save = (save: object) => postMessage({ type: 'lift_save', ...save });
|
|
31
31
|
|
|
32
|
+
/** Results of a batch the page asked for in a reply, before an animation. Waits for the call's next reply. */
|
|
33
|
+
const batch_done = (results: unknown) => request({ type: 'batch_done', results });
|
|
34
|
+
|
|
32
35
|
const ready = (async () => {
|
|
33
36
|
const { loadPyodide } = await import(/* @vite-ignore */ `${BASE}pyodide.mjs`);
|
|
34
37
|
const py = await loadPyodide({ indexURL: BASE, env: { MPLBACKEND: 'module://pie_mpl' } });
|
|
35
38
|
py.setStdout({ batched: (text: string) => postMessage({ type: 'output', text: text + '\n' }) });
|
|
36
39
|
py.setStderr({ batched: (text: string) => postMessage({ type: 'output', text: text + '\n' }) });
|
|
37
40
|
py.registerJsModule('bridge', {
|
|
38
|
-
call, item, lift, lift_save, plot: (png: string) => postMessage({ type: 'plot', png }),
|
|
41
|
+
call, item, lift, lift_save, batch_done, plot: (png: string) => postMessage({ type: 'plot', png }),
|
|
39
42
|
});
|
|
40
43
|
py.FS.writeFile('/home/pyodide/pie.py', PIE_PY);
|
|
41
44
|
py.FS.writeFile('/home/pyodide/pie_mpl.py', PIE_MPL_PY);
|
|
@@ -65,6 +68,10 @@ self.onmessage = async ({ data }) => {
|
|
|
65
68
|
py.runPython('import pie', { globals });
|
|
66
69
|
scopes.set(data.scope, globals);
|
|
67
70
|
}
|
|
71
|
+
// A batch finds lifted functions in the running block's globals.
|
|
72
|
+
const pie = py.pyimport('pie');
|
|
73
|
+
pie._scope = globals;
|
|
74
|
+
pie.destroy();
|
|
68
75
|
// Downloads packages the code imports (numpy, scipy, matplotlib, ...) on first use.
|
|
69
76
|
await py.loadPackagesFromImports(data.code);
|
|
70
77
|
try {
|