Skip to content

Add explicit Pyodide extension API and clock example (AI-Assisted) - #353

Open
bh0fer wants to merge 5 commits into
mainfrom
feat/pyodide-extension-api
Open

bh0fer wants to merge 5 commits into
mainfrom
feat/pyodide-extension-api

Conversation

@bh0fer

@bh0fer bh0fer commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Websites can now register Python-controlled React integrations through explicit Pyodide plugin options. The clock showcase uses worker/client entry points and a shared payload type, replacing its package-file override and central SiteStore.handleMessage switch while preserving from clock import use_clock and the existing React view.

API and clock example

import type { PluginOptions } from '@tdev/pyodide-code/plugin';

// Inside siteConfig.plugins:
[
    require.resolve('@tdev/pyodide-code/plugin'),
    {
        extensions: [
            {
                name: 'clock',
                worker: require.resolve('./website/pyodide/clock/worker'),
                client: require.resolve('./website/pyodide/clock/client')
            }
        ]
    } satisfies PluginOptions
]

defineWorkerExtension<Payload>() creates Python modules per execution and provides send(targetId, payload). defineClientExtension<Payload>() receives the current root store and typed delivery within the existing MobX action. The plugin generates separate worker/browser registries, including SSR aliases. Module names are independent of extension names and event payloads; duplicate/reserved names fail explicitly.

The executable clock example is in tdev-website/website/pyodide/clock/{protocol,worker,client}.ts. The package README includes the actual example source, registration, migration steps, and lifecycle semantics. Existing overrides and custom SiteStore messages remain supported during migration.

Design re-evaluation and review

  • Chose static generated imports over global startup registration to avoid ordering and an additional store lifecycle.
  • Kept React rendering and store ownership with the website. Discovery, generic state synchronization, and bidirectional RPC remain outside this change.
  • Preserved partial clock updates: a new Python handle updating seconds must not reset hours/minutes written by another handle or a previous execution. set_time and reset each deliver one update.
  • Independent review caught async callbacks accepted by a void signature, cleanup after a later factory fails, and early rejection while other messages remain pending. The final API enforces synchronous handlers; execution cleanup closes contexts and drains all pending deliveries, including setup failures.
  • Review verdict: Proceed; moderate maintenance burden. Remaining limits are documented: shared payload types are trusted rather than runtime-validated; clock getters describe their own handle; cached Python helpers must re-import factories per execution; extensions own their background work.

Validation

  • yarn typecheck — passed, including compile-time payload, registry separation, and synchronous-handler assertions.
  • yarn test --run — 23 files, 219 tests passed; 19 new extension configuration/lifecycle cases.
  • yarn build — passed, including SSR and emitted module worker.
  • yarn format:check and git diff --check — passed.
  • Chromium validation using actual Pyodide, Comlink, ClockStore, and the existing clock React component — passed for both the development bundle and emitted production worker. Checked dynamic imports, fresh contexts, editor/target routing, repeat/concurrent executions, partial updates/reset, handler error/recovery, unknown extension errors, hard stop/restart, input, and completion after a 200-message burst. For offline validation only, the production worker's Pyodide CDN base URL was redirected to the same installed local Pyodide assets.

The clock's appearance is unchanged.

@bh0fer
bh0fer requested a review from a team October 27, 2026 21:40
@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@netlify

netlify Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for teaching-dev ready!

Name Link
🔨 Latest commit e6aaca5
🔍 Latest deploy log https://app.netlify.com/projects/teaching-dev/deploys/6ae2134d4c2352b4def6de79
😎 Deploy Preview https://deploy-preview-353--teaching-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@lebalz
lebalz added this pull request to stack #355 October 28, 2026 18:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants