@tinyvolt/pie 0.2.0 → 0.5.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 +8 -0
- package/init/index.js +4 -0
- package/init/postinstall.js +38 -0
- package/init/site-agents.md +18 -5
- package/package.json +4 -2
- package/pages/_topics.md +7 -0
- package/pages/about.md +1 -0
- package/pages/example.md +37 -3
- package/pages/python-editor.md +47 -16
- package/public/logo.svg +1 -1
- package/src/app.css +129 -0
- package/src/lib/markdown/features/build.ts +73 -0
- package/src/lib/markdown/features/sidebar/View.tsx +56 -0
- package/src/lib/markdown/features/sidebar/build.ts +8 -0
- package/src/lib/markdown/features/toc/build.ts +71 -0
- package/src/lib/markdown/features/views.tsx +20 -0
- package/src/lib/markdown/frontmatter.ts +28 -3
- package/src/lib/markdown/pages.ts +4 -1
- package/src/lib/markdown/render.ts +37 -5
- package/src/lib/markdown/vitePlugin.ts +27 -4
- 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/src/lib/site/vitePlugin.ts +3 -0
- package/src/routes/[...slug].tsx +4 -2
- package/styles/theme.css +3 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The view part of every feature: runs in the browser. Each feature's build part is in `build.ts`.
|
|
2
|
+
import type { Component, JSX } from 'solid-js';
|
|
3
|
+
import type { FeatureData } from './build';
|
|
4
|
+
import { SidebarView } from './sidebar/View';
|
|
5
|
+
|
|
6
|
+
/** A feature's view: wraps the page, given the feature's data. */
|
|
7
|
+
type FeatureView<D> = Component<{ data: D; children: JSX.Element }>;
|
|
8
|
+
|
|
9
|
+
/** Every feature's view, by key, outermost first. A feature with no view, like `toc`, is left out. */
|
|
10
|
+
const VIEWS: { [K in keyof FeatureData]: FeatureView<NonNullable<FeatureData[K]>> } = {
|
|
11
|
+
sidebar: SidebarView,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** `body` wrapped in the view of every feature that is on for the page, the first in `VIEWS` outermost. */
|
|
15
|
+
export function wrapPage(data: FeatureData, body: JSX.Element): JSX.Element {
|
|
16
|
+
return (Object.entries(VIEWS) as [keyof FeatureData, FeatureView<unknown>][]).reduceRight<JSX.Element>(
|
|
17
|
+
(inner, [key, View]) => (data[key] === undefined ? inner : <View data={data[key]}>{inner}</View>),
|
|
18
|
+
body,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
@@ -23,6 +23,28 @@ export function text(fallback: string): Field<string> {
|
|
|
23
23
|
return { default: fallback, parse: raw => raw };
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/** `true` or `false`. */
|
|
27
|
+
export function boolean(fallback: boolean): Field<boolean> {
|
|
28
|
+
return {
|
|
29
|
+
default: fallback,
|
|
30
|
+
parse(raw) {
|
|
31
|
+
if (raw !== 'true' && raw !== 'false') throw new Error(`must be true or false, got ${JSON.stringify(raw)}`);
|
|
32
|
+
return raw === 'true';
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A path to a `.md` file, or `none`. Empty when absent. */
|
|
38
|
+
export function markdownFile(): Field<string> {
|
|
39
|
+
return {
|
|
40
|
+
default: '',
|
|
41
|
+
parse(raw) {
|
|
42
|
+
if (raw !== 'none' && !raw.endsWith('.md')) throw new Error(`must be a .md file or none, got ${JSON.stringify(raw)}`);
|
|
43
|
+
return raw;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
26
48
|
/** Every frontmatter field a page can set. Keys not listed here are ignored. */
|
|
27
49
|
const FIELDS = {
|
|
28
50
|
canvas: oneOf(['left', 'right', 'top', 'bottom', 'none'], 'none'),
|
|
@@ -72,11 +94,14 @@ function splitFrontmatter(markdown: string): { raw: Record<string, string>; body
|
|
|
72
94
|
return { raw: parseKeyValues(match[1]), body: markdown.slice(match[0].length) };
|
|
73
95
|
}
|
|
74
96
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Reads the frontmatter of the file at `path`. `raw`: every key it sets, unchecked, for the features to read.
|
|
99
|
+
* Throws, naming `path`, on a malformed block or a bad value.
|
|
100
|
+
*/
|
|
101
|
+
export function parseFrontmatter(path: string, markdown: string): { frontmatter: Frontmatter; raw: Record<string, string>; body: string } {
|
|
77
102
|
try {
|
|
78
103
|
const { raw, body } = splitFrontmatter(markdown);
|
|
79
|
-
return { frontmatter: readFields(FIELDS, raw), body };
|
|
104
|
+
return { frontmatter: readFields(FIELDS, raw), raw, body };
|
|
80
105
|
} catch (e) {
|
|
81
106
|
throw new Error(`${path}: ${(e as Error).message}`);
|
|
82
107
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
import type { FeatureData } from './features/build';
|
|
1
2
|
import type { Frontmatter } from './frontmatter';
|
|
2
3
|
|
|
3
4
|
/** One `pages/**\/*.md`, as `vitePlugin.ts` turns it into a module at build time. */
|
|
4
5
|
export interface Page {
|
|
5
6
|
frontmatter: Frontmatter;
|
|
6
7
|
html: string;
|
|
8
|
+
features: FeatureData;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
// Not eager: each page is its own file, downloaded only when that page is opened.
|
|
10
|
-
|
|
12
|
+
// A file whose name starts with `_` is not a page; it can still be used by a feature, e.g. as a sidebar.
|
|
13
|
+
const loaders = import.meta.glob<Page>(['/pages/**/*.md', '!/pages/**/_*.md'], { import: 'default' });
|
|
11
14
|
|
|
12
15
|
// Slug -> loader. `/pages/a/b.md` has the slug `a/b`.
|
|
13
16
|
const bySlug = new Map(
|
|
@@ -1,11 +1,34 @@
|
|
|
1
1
|
import katex from 'katex';
|
|
2
|
-
import { Marked, Renderer } from 'marked';
|
|
2
|
+
import { Marked, Renderer, type MarkedExtension } 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
|
|
|
8
|
-
|
|
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
|
+
|
|
31
|
+
const BASE: MarkedExtension = {
|
|
9
32
|
extensions: [
|
|
10
33
|
{
|
|
11
34
|
// `$$…$$` that fills its own lines.
|
|
@@ -49,14 +72,23 @@ 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
|
},
|
|
57
85
|
},
|
|
58
|
-
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const marked = new Marked(BASE);
|
|
59
89
|
|
|
60
|
-
|
|
61
|
-
|
|
90
|
+
/** `extensions`: markdown syntax the page's features add. */
|
|
91
|
+
export function renderMarkdown(markdown: string, extensions: MarkedExtension[] = []): string {
|
|
92
|
+
const parser = extensions.length === 0 ? marked : new Marked(BASE, ...extensions);
|
|
93
|
+
return parser.parse(markdown) as string;
|
|
62
94
|
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import type { Plugin } from 'vite';
|
|
4
|
+
import { buildFeatures } from './features/build';
|
|
4
5
|
import { parseFrontmatter } from './frontmatter';
|
|
5
6
|
import type { Page } from './pages';
|
|
6
7
|
import { renderMarkdown } from './render';
|
|
7
8
|
import { APP_DIR } from '../appDir';
|
|
9
|
+
import { parseSite } from '../site/vitePlugin';
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* Turns each `pages/**\/*.md` into a module whose default export is a `Page`, at build time,
|
|
11
13
|
* so the browser gets finished HTML and never runs the markdown tools.
|
|
12
|
-
* Bad frontmatter fails the build; in dev it shows Vite's error screen.
|
|
14
|
+
* Bad frontmatter, or a feature's bad value, fails the build; in dev it shows Vite's error screen.
|
|
13
15
|
*/
|
|
14
16
|
export function markdownPages(): Plugin {
|
|
15
17
|
let root = '';
|
|
@@ -23,8 +25,27 @@ export function markdownPages(): Plugin {
|
|
|
23
25
|
transform(markdown, id) {
|
|
24
26
|
const file = path.relative(root, id.split('?')[0]);
|
|
25
27
|
if (!file.startsWith(`pages${path.sep}`) || !file.endsWith('.md')) return;
|
|
26
|
-
const { frontmatter, body } = parseFrontmatter(file, markdown);
|
|
27
|
-
|
|
28
|
+
const { frontmatter, raw, body } = parseFrontmatter(file, markdown);
|
|
29
|
+
|
|
30
|
+
// Features read `site.yml` for the keys a page leaves out, so editing it rebuilds every page.
|
|
31
|
+
const siteFile = path.join(root, 'site.yml');
|
|
32
|
+
this.addWatchFile(siteFile);
|
|
33
|
+
const site = parseSite('site.yml', fs.readFileSync(siteFile, 'utf8'));
|
|
34
|
+
|
|
35
|
+
const renderFile = (from: string, mdPath: string) => {
|
|
36
|
+
const target = path.resolve(root, path.dirname(from), mdPath);
|
|
37
|
+
const relative = path.relative(root, target);
|
|
38
|
+
if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
39
|
+
throw new Error(`must be inside the project, got ${JSON.stringify(mdPath)}`);
|
|
40
|
+
}
|
|
41
|
+
const shown = relative.split(path.sep).join('/');
|
|
42
|
+
if (!fs.existsSync(target)) throw new Error(`file ${shown} does not exist`);
|
|
43
|
+
this.addWatchFile(target);
|
|
44
|
+
return renderMarkdown(parseFrontmatter(shown, fs.readFileSync(target, 'utf8')).body);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const { data, extensions } = buildFeatures(file, raw, site, renderFile);
|
|
48
|
+
const page: Page = { frontmatter, html: renderMarkdown(body, extensions), features: data };
|
|
28
49
|
return { code: `export default ${JSON.stringify(page)};`, map: null };
|
|
29
50
|
},
|
|
30
51
|
};
|
|
@@ -56,7 +77,9 @@ export function checkPageRoutes(): Plugin {
|
|
|
56
77
|
.filter(file => /\.(tsx|ts|jsx|js)$/.test(file) && file !== PAGE_ROUTE)
|
|
57
78
|
.map(file => ({ file, pattern: routePattern(file) }));
|
|
58
79
|
const clashes: string[] = [];
|
|
59
|
-
|
|
80
|
+
// A file whose name starts with `_` has no URL.
|
|
81
|
+
const pages = listFiles(path.join(root, 'pages')).filter(file => file.endsWith('.md') && !path.posix.basename(file).startsWith('_'));
|
|
82
|
+
for (const file of pages) {
|
|
60
83
|
const url = file.replace(/\.md$/, '').split('/');
|
|
61
84
|
for (const route of routes) {
|
|
62
85
|
if (matches(route.pattern, url)) {
|
|
@@ -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
|