@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.
@@ -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,5 +1,6 @@
1
1
  import path from 'node:path';
2
2
  import type { Plugin } from 'vite';
3
+ import { FEATURE_FIELDS } from '../markdown/features/build';
3
4
  import { parseKeyValues, readFields, text, type Field, type Values } from '../markdown/frontmatter';
4
5
 
5
6
  /** An `http://` or `https://` address, without a trailing `/`. */
@@ -21,6 +22,8 @@ const FIELDS = {
21
22
  url: address(),
22
23
  /** The search-result description of a page that has no `description` of its own. */
23
24
  description: text(''),
25
+ /** Each feature's frontmatter key, as the value of pages that leave it out. Paths are relative to the project root. */
26
+ ...FEATURE_FIELDS,
24
27
  };
25
28
 
26
29
  export type Site = Values<typeof FIELDS>;
@@ -6,6 +6,7 @@ import { PageMeta } from '~/components/PageMeta';
6
6
  import { PageTitle } from '~/components/PageTitle';
7
7
  import { CanvasPage } from '~/lib/markdown/components/CanvasPage';
8
8
  import { MarkdownBody } from '~/lib/markdown/components/MarkdownBody';
9
+ import { wrapPage } from '~/lib/markdown/features/views';
9
10
  import { loadPage } from '~/lib/markdown/pages';
10
11
 
11
12
  export default function Page() {
@@ -13,7 +14,7 @@ export default function Page() {
13
14
  const page = createAsync(() => loadPage(params.slug ?? ''));
14
15
 
15
16
  // `undefined` while loading, `null` when there is no such page.
16
- // Keyed: a new page gets a fresh canvas and fresh Python variables.
17
+ // Keyed: a new page gets a fresh canvas, fresh Python variables and fresh features, e.g. a collapsed sidebar.
17
18
  return (
18
19
  <Switch>
19
20
  <Match when={page() === null}>
@@ -26,10 +27,11 @@ export default function Page() {
26
27
  <Match when={page()} keyed>
27
28
  {(page) => {
28
29
  const canvas = page.frontmatter.canvas;
30
+ const body = canvas === 'none' ? <MarkdownBody html={page.html} /> : <CanvasPage side={canvas} html={page.html} />;
29
31
  return (
30
32
  <>
31
33
  <PageMeta title={page.frontmatter.title} description={page.frontmatter.description} />
32
- {canvas === 'none' ? <MarkdownBody html={page.html} /> : <CanvasPage side={canvas} html={page.html} />}
34
+ {wrapPage(page.features, body)}
33
35
  </>
34
36
  );
35
37
  }}
package/styles/theme.css CHANGED
@@ -14,6 +14,7 @@
14
14
  --color-border-hover: #999;
15
15
  --color-code-bg: #f4f4f4;
16
16
  --color-error: #c00;
17
+ --color-backdrop: rgb(0 0 0 / 0.3); /* behind the open sidebar on narrow screens */
17
18
 
18
19
  /* Fonts */
19
20
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; /* header, editor */
@@ -24,4 +25,6 @@
24
25
 
25
26
  /* Layout */
26
27
  --content-width: 40rem; /* page text column, about 70 characters */
28
+ --sidebar-width: 16rem;
29
+ --sidebar-drawer-below: 768px; /* on narrower screens the sidebar opens over the page */
27
30
  }