From 81b390f4a57746db31a27aad2bc7091b6be4565e Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 29 Sep 2026 09:58:41 -0700 Subject: [PATCH 1/8] fix(playground): generate Python clients from shared samples Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../python-browser-wheel-assets-2026-09-28.md | 8 + packages/http-client-python/README.md | 10 + .../emitter/src/browser-runtime.ts | 64 ++++++ .../emitter/src/constants.ts | 1 - .../http-client-python/emitter/src/emitter.ts | 27 +-- .../emitter/src/pyodide-loader.browser.ts | 2 +- .../emitter/src/pyodide-loader.ts | 2 +- .../emitter/test/browser-runtime.test.ts | 122 +++++++++++ .../eng/scripts/setup/build_pygen_wheel.py | 207 +++++++++++++++++- .../generator/browser-requirements.txt | 11 + packages/http-client-python/package.json | 32 +++ .../src/react/use-playground-state.ts | 9 +- .../test/use-playground-state.test.ts | 54 +++++ 13 files changed, 517 insertions(+), 32 deletions(-) create mode 100644 .chronus/changes/python-browser-wheel-assets-2026-09-28.md create mode 100644 packages/http-client-python/emitter/src/browser-runtime.ts create mode 100644 packages/http-client-python/emitter/test/browser-runtime.test.ts create mode 100644 packages/http-client-python/generator/browser-requirements.txt create mode 100644 packages/playground/test/use-playground-state.test.ts diff --git a/.chronus/changes/python-browser-wheel-assets-2026-09-28.md b/.chronus/changes/python-browser-wheel-assets-2026-09-28.md new file mode 100644 index 00000000000..82fb52436c6 --- /dev/null +++ b/.chronus/changes/python-browser-wheel-assets-2026-09-28.md @@ -0,0 +1,8 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" + - "@typespec/playground" +--- + +Generate Python clients from shared Playground links by preserving their emitter configuration and loading Pyodide and browser-compatible wheels from published package assets instead of fetching dependencies from public package services. diff --git a/packages/http-client-python/README.md b/packages/http-client-python/README.md index 0c3335df5fa..49acd783839 100644 --- a/packages/http-client-python/README.md +++ b/packages/http-client-python/README.md @@ -2,6 +2,16 @@ TypeSpec emitter for Python SDKs +In the browser Playground, the emitter loads Pyodide and the Python generator from +TypeSpec's versioned package assets. The build copies Pyodide's runtime files from the +installed npm package and downloads its bootstrap packages and generator dependencies from +the configured Python package feed. Configure both npm and Python to use the approved CFS +feeds when building these assets. PyYAML and MarkupSafe are built as pure-Python wheels +from source so they work in Pyodide without native extensions. Visitors' browsers do not +query an external package CDN or index. Keep `generator/browser-requirements.txt` aligned +with `generator/setup.py` when upgrading the generator, and configure both `UV_DEFAULT_INDEX` +and `PIP_INDEX_URL` for the intended package feed when building with uv. + ## Install ```bash diff --git a/packages/http-client-python/emitter/src/browser-runtime.ts b/packages/http-client-python/emitter/src/browser-runtime.ts new file mode 100644 index 00000000000..4285758bff3 --- /dev/null +++ b/packages/http-client-python/emitter/src/browser-runtime.ts @@ -0,0 +1,64 @@ +import pkgJson from "../../package.json" with { type: "json" }; +import { BLOB_STORAGE_BASE_URL, PACKAGE_NAME, PYGEN_WHEEL_FILENAME } from "./constants.js"; +import type { PyodideInterface } from "./pyodide-loader.js"; +import { loadPyodide } from "./pyodide-loader.js"; + +export async function setupPyodideCallBrowser(): Promise { + const assetsUrl = `${BLOB_STORAGE_BASE_URL}/${PACKAGE_NAME}/${pkgJson.version}/generator/dist`; + const pyodide = await loadPyodide({ + indexURL: `${assetsUrl}/pyodide/`, + }); + + pyodide.FS.mkdirTree("/generator"); + const response = await fetch(`${assetsUrl}/browser-wheels.json`); + if (!response.ok) { + throw new Error(`Failed to load browser Python wheels: ${response.status}`); + } + const manifest: unknown = await response.json(); + const isWheelList = (value: unknown): value is string[] => + Array.isArray(value) && + value.length > 0 && + value.every( + (name) => + typeof name === "string" && /^[A-Za-z0-9_]+-[A-Za-z0-9_.-]+-py3-none-any\.whl$/.test(name), + ); + const generatorWheels = + typeof manifest === "object" && manifest !== null && "generator" in manifest + ? manifest.generator + : undefined; + const pyodideWheels = + typeof manifest === "object" && manifest !== null && "pyodide" in manifest + ? manifest.pyodide + : undefined; + if ( + !isWheelList(generatorWheels) || + !isWheelList(pyodideWheels) || + pyodideWheels.length !== 5 || + !["packaging", "micropip", "click", "pyyaml", "markupsafe"].every((name, index) => + pyodideWheels[index].startsWith(`${name}-`), + ) + ) { + throw new Error("Invalid browser Python wheel manifest"); + } + + const pyodideWheelUrls = pyodideWheels.map((name) => `${assetsUrl}/pyodide-wheels/${name}`); + await pyodide.loadPackage(pyodideWheelUrls.slice(0, 2)); + + const globals = pyodide.toPy({ + pyodideWheelUrls: pyodideWheelUrls.slice(2), + wheelUrls: generatorWheels.map((name) => `${assetsUrl}/browser-wheels/${name}`), + generatorWheelUrl: `${assetsUrl}/${PYGEN_WHEEL_FILENAME}`, + }); + try { + await pyodide.runPythonAsync( + `import micropip +await micropip.install(pyodideWheelUrls, deps=False) +await micropip.install(wheelUrls, deps=False) +await micropip.install(generatorWheelUrl, deps=False)`, + { globals }, + ); + } finally { + globals.destroy(); + } + return pyodide; +} diff --git a/packages/http-client-python/emitter/src/constants.ts b/packages/http-client-python/emitter/src/constants.ts index 15bec92c979..49911db9845 100644 --- a/packages/http-client-python/emitter/src/constants.ts +++ b/packages/http-client-python/emitter/src/constants.ts @@ -1,4 +1,3 @@ -export const PYODIDE_VERSION = "0.26.2"; export const PYGEN_WHEEL_FILENAME = "pygen-0.1.0-py3-none-any.whl"; export const BLOB_STORAGE_BASE_URL = "https://typespec.blob.core.windows.net/pkgs"; export const PACKAGE_NAME = "@typespec/http-client-python"; diff --git a/packages/http-client-python/emitter/src/emitter.ts b/packages/http-client-python/emitter/src/emitter.ts index c68517f99f4..3ed8eedf739 100644 --- a/packages/http-client-python/emitter/src/emitter.ts +++ b/packages/http-client-python/emitter/src/emitter.ts @@ -1,26 +1,15 @@ import { createSdkContext } from "@azure-tools/typespec-client-generator-core"; import type { EmitContext } from "@typespec/compiler"; import { emitFile, joinPaths, listServices, NoTarget } from "@typespec/compiler"; -import pkgJson from "../../package.json" with { type: "json" }; +import { setupPyodideCallBrowser } from "./browser-runtime.js"; import { emitCodeModel } from "./code-model.js"; -import { - BLOB_STORAGE_BASE_URL, - PACKAGE_NAME, - PYGEN_WHEEL_FILENAME, - PYODIDE_VERSION, -} from "./constants.js"; import type { PythonEmitterOptions, PythonSdkContext } from "./lib.js"; import { reportDiagnostic } from "./lib.js"; import { runNodeEmit } from "./node-runner.js"; import type { PyodideInterface } from "./pyodide-loader.js"; -import { loadPyodide } from "./pyodide-loader.js"; import { getRootNamespace, md2Rst } from "./utils.js"; import { dumpCodeModelToYaml } from "./yaml-utils.js"; -function getBrowserPygenWheelUrl(): string { - return `${BLOB_STORAGE_BASE_URL}/${PACKAGE_NAME}/${pkgJson.version}/generator/dist/${PYGEN_WHEEL_FILENAME}`; -} - function addDefaultOptions(sdkContext: PythonSdkContext) { const defaultOptions = { "package-version": "1.0.0b1", @@ -297,17 +286,3 @@ function clearMemfsDirectory(pyodide: PyodideInterface, dir: string): void { } } } - -async function setupPyodideCallBrowser() { - const pyodide = await loadPyodide({ - indexURL: `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`, - }); - - // use default MEMFS for browser, since NODEFS is not supported - pyodide.FS.mkdirTree("/generator"); - await pyodide.loadPackage("micropip"); - const micropip = pyodide.pyimport("micropip"); - await micropip.install(getBrowserPygenWheelUrl()); - - return pyodide; -} diff --git a/packages/http-client-python/emitter/src/pyodide-loader.browser.ts b/packages/http-client-python/emitter/src/pyodide-loader.browser.ts index e0a7cde43e8..59ffd948cd0 100644 --- a/packages/http-client-python/emitter/src/pyodide-loader.browser.ts +++ b/packages/http-client-python/emitter/src/pyodide-loader.browser.ts @@ -1,5 +1,5 @@ // Browser stub for `pyodide-loader.ts`. Loads pyodide via a `