Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/python-module-loading-2026-3-29-14-53-8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-python"
---

Update Python emitter browser behavior to only load Pyodide once instead of on every emit
31 changes: 30 additions & 1 deletion packages/http-client-python/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,21 @@ async function onEmitMain(context: EmitContext<PythonEmitterOptions>) {

if (typeof window !== "undefined") {
// Running in browser with Pyodide - fileURLToPath and other filesystem operations are browser-incompatible
const pyodide = await setupPyodideCallBrowser();
const pyodide = await browserPyodidePromise;

if (!pyodide) {
reportDiagnostic(program, {
code: "browser-runtime-load-failed",
target: NoTarget,
format: { details: "" },
});
return;
}

const yamlFilePath = "/yaml/python-yaml-path.yaml";
pyodide.FS.mkdirTree("/yaml");
pyodide.FS.mkdirTree("/output");
clearMemfsDirectory(pyodide, "/output");
pyodide.FS.writeFile(yamlFilePath, jsyaml.dump(parsedYamlMap));

await runPyodideGeneration(pyodide, "/output", yamlFilePath, commandArgs);
Expand Down Expand Up @@ -325,6 +335,25 @@ async function onEmitMain(context: EmitContext<PythonEmitterOptions>) {
}
}

const browserPyodidePromise: Promise<PyodideInterface> | null =
typeof window !== "undefined" ? setupPyodideCallBrowser() : null;

function clearMemfsDirectory(pyodide: PyodideInterface, dir: string): void {
const entries: string[] = pyodide.FS.readdir(dir).filter(
(entry: string) => entry !== "." && entry !== "..",
);
for (const entry of entries) {
const fullPath = `${dir}/${entry}`;
const stats = pyodide.FS.stat(fullPath);
if (pyodide.FS.isDir(stats.mode)) {
clearMemfsDirectory(pyodide, fullPath);
pyodide.FS.rmdir(fullPath);
} else {
pyodide.FS.unlink(fullPath);
}
}
}

async function setupPyodideCallBrowser() {
const pyodide = await loadPyodide({
indexURL: `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const WebsitePlayground = ({ versionData }: WebsitePlaygroundProps) => {
libraries={imports}
emitterViewers={{ "@typespec/openapi3": [SwaggerUIViewer] }}
emitterOptions={{
"@typespec/http-client-python": { debounce: 500 },
"@typespec/http-client-python": { debounce: 1000 },
"@typespec/http-client-csharp": { debounce: 500 },
}}
importConfig={{ useShim: true }}
Expand Down
Loading