@tinyvolt/pie 0.1.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.
@@ -1,10 +1,33 @@
1
1
  import katex from 'katex';
2
- import { Marked } from 'marked';
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
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
  {
@@ -47,6 +70,19 @@ const marked = new Marked({
47
70
  },
48
71
  },
49
72
  ],
73
+ renderer: {
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.
76
+ code(token) {
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
+ }
82
+ if (lang !== 'python-editor' || !flags.includes('collapsed')) return false;
83
+ return new Renderer().code(token).replace('<code ', '<code data-collapsed ');
84
+ },
85
+ },
50
86
  });
51
87
 
52
88
  export function renderMarkdown(markdown: string): string {
@@ -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
- {gridLines()}
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 width={viewWidth()} height={viewHeight()} unit={props.store.unit()}>
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 */}
@@ -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
- export async function execute(store: NodeStore, keyword: string, args: Arg[], { out, hidden, fill, stroke, animate }: CallOptions = {}): Promise<ExecuteResult> {
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
- await Promise.all(runs);
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
- /** The call waits for the returned promise, e.g. until an animation ends. */
48
- run: (inputs: AnyNode[], store: NodeStore, animate: boolean) => void | Promise<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 move to their new values over `ANIMATION_MS` instead of jumping.
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): Promise<void> {
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 Promise.resolve();
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 new Promise((resolve) => createRoot((dispose) => {
32
- const start = performance.now();
25
+ return createRoot((dispose) => {
33
26
  const [progress, setProgress] = createSignal(0);
34
- store.saveNode(node.id, createMemo(() => (progress() < 1 ? at(easeInOut(progress())) : end)), dispose);
35
- let frame = requestAnimationFrame(function step() {
36
- setProgress(Math.min((performance.now() - start) / ANIMATION_MS, 1));
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
- cancelAnimationFrame(frame);
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() {
@@ -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
+ }
@@ -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(run_sync(bridge.call(name, js_args, js_options)))
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
@@ -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: data.reqId, ...msg });
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
- execute(store, data.keyword, data.args, data.options).then(
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. */
@@ -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 {
@@ -1,17 +1,32 @@
1
1
  import path from 'node:path';
2
2
  import type { Plugin } from 'vite';
3
- import { parseKeyValues, readFields, text, type Values } from '../markdown/frontmatter';
3
+ import { parseKeyValues, readFields, text, type Field, type Values } from '../markdown/frontmatter';
4
+
5
+ /** An `http://` or `https://` address, without a trailing `/`. */
6
+ function address(): Field<string> {
7
+ return {
8
+ default: '',
9
+ parse(raw) {
10
+ if (!/^https?:\/\/[^\s/]+/.test(raw)) throw new Error(`must start with http:// or https://, got ${JSON.stringify(raw)}`);
11
+ return raw.replace(/\/+$/, '');
12
+ },
13
+ };
14
+ }
4
15
 
5
16
  /** Every field `site.yml` can set. Keys not listed here are ignored. */
6
17
  const FIELDS = {
7
18
  /** Shown in the header and in every browser tab title. */
8
19
  title: text(''),
20
+ /** Where the site is deployed, e.g. `https://example.com`. Empty: no sitemap, robots.txt, canonical links or `og:url`. */
21
+ url: address(),
22
+ /** The search-result description of a page that has no `description` of its own. */
23
+ description: text(''),
9
24
  };
10
25
 
11
26
  export type Site = Values<typeof FIELDS>;
12
27
 
13
28
  /** Reads `site.yml`. Throws, naming the file, on a bad line or a missing `title`. */
14
- function parseSite(file: string, yml: string): Site {
29
+ export function parseSite(file: string, yml: string): Site {
15
30
  try {
16
31
  const site = readFields(FIELDS, parseKeyValues(yml));
17
32
  if (!site.title) throw new Error('`title` is required');
@@ -2,6 +2,7 @@ import { createAsync, useParams } from '@solidjs/router';
2
2
  import { HttpStatusCode } from '@solidjs/start';
3
3
  import { Match, Switch } from 'solid-js';
4
4
  import 'katex/dist/katex.min.css';
5
+ import { PageMeta } from '~/components/PageMeta';
5
6
  import { PageTitle } from '~/components/PageTitle';
6
7
  import { CanvasPage } from '~/lib/markdown/components/CanvasPage';
7
8
  import { MarkdownBody } from '~/lib/markdown/components/MarkdownBody';
@@ -27,7 +28,7 @@ export default function Page() {
27
28
  const canvas = page.frontmatter.canvas;
28
29
  return (
29
30
  <>
30
- <PageTitle>{page.frontmatter.title}</PageTitle>
31
+ <PageMeta title={page.frontmatter.title} description={page.frontmatter.description} />
31
32
  {canvas === 'none' ? <MarkdownBody html={page.html} /> : <CanvasPage side={canvas} html={page.html} />}
32
33
  </>
33
34
  );
@@ -1,3 +1,4 @@
1
+ import { PageMeta } from "~/components/PageMeta";
1
2
  import { layout, type LayoutModules } from "~/lib/layouts/layout";
2
3
 
3
4
  // Shown when the site has no `layouts/index.tsx`.
@@ -11,4 +12,13 @@ function Home() {
11
12
  );
12
13
  }
13
14
 
14
- export default layout(import.meta.glob<LayoutModules[string]>("/layouts/index.tsx", { eager: true }), Home);
15
+ const Layout = layout(import.meta.glob<LayoutModules[string]>("/layouts/index.tsx", { eager: true }), Home);
16
+
17
+ export default function Index() {
18
+ return (
19
+ <>
20
+ <PageMeta />
21
+ <Layout />
22
+ </>
23
+ );
24
+ }
@@ -1,11 +1,11 @@
1
1
  import { For } from 'solid-js';
2
- import { PageTitle } from '~/components/PageTitle';
2
+ import { PageMeta } from '~/components/PageMeta';
3
3
  import { slugs } from '~/lib/markdown/pages';
4
4
 
5
5
  export default function Pages() {
6
6
  return (
7
7
  <main>
8
- <PageTitle>Pages</PageTitle>
8
+ <PageMeta title="Pages" />
9
9
  <ul>
10
10
  <For each={slugs}>
11
11
  {slug => (