playground: basic editor functionality ready

This commit is contained in:
Yeicor
2025-07-19 21:49:02 +02:00
parent 667a08d2c6
commit fc32393635
10 changed files with 1115 additions and 380 deletions

View File

@@ -0,0 +1,27 @@
import {loadPyodide, version} from "pyodide";
let pyodideReadyPromise = loadPyodide({
indexURL: `https://cdn.jsdelivr.net/pyodide/v${version}/full/`, // FIXME: Local deployment?
packages: ["micropip", "sqlite3"], // Preloaded faster here...
stdout: (msg) => self.postMessage({stdout: msg}),
stderr: (msg) => self.postMessage({stderr: msg}),
stdin: () => {
console.warn("Input requested by Python code, but stdin is not supported in this playground.");
return "";
},
});
self.onmessage = async (event) => {
// make sure loading is done
const pyodide = await pyodideReadyPromise;
const {id, code} = event.data;
// Now load any packages we need, run the code, and send the result back.
await pyodide.loadPackagesFromImports(code);
try {
// Execute the python code in this context
const result = await pyodide.runPythonAsync(code);
self.postMessage({result, id});
} catch (error: any) {
self.postMessage({error: error.message, id});
}
};