[web] include output tracking in run-finished lifecycle event in pyodide

GitOrigin-RevId: 5c72abc35ea4045f9c6aa374a2c51490f8f6cd38
This commit is contained in:
Domagoj Kriskovic
2026-04-14 08:04:13 +00:00
committed by Copybot
parent b893ba36e2
commit 7d032e73d6
4 changed files with 80 additions and 13 deletions
@@ -14,7 +14,7 @@ export type LifecycleCallback = (
event:
| { type: 'loaded' }
| { type: 'loading-failed'; error: string }
| { type: 'run-finished'; requestId: string }
| { type: 'run-finished'; requestId: string; outputs: string[] }
) => void
export class PyodideWorkerClient {
@@ -172,6 +172,7 @@ export class PyodideWorkerClient {
this.lifecycleCallback?.({
type: 'run-finished',
requestId: response.id,
outputs: response.outputs,
})
break
}
@@ -43,6 +43,7 @@ export type PyodideWorkerEvent =
export type RunCodeResult = {
type: 'run-code-result'
id: string
outputs: string[]
}
export type PyodideWorkerResponse = PyodideWorkerEvent | RunCodeResult
@@ -10,6 +10,7 @@ type PyodideFS = PyodideInterface['FS']
type PyodideModule = typeof import('pyodide')
const PROJECT_FS_ROOT = '/project'
const PROJECT_FS_PREFIX = `${PROJECT_FS_ROOT}/`
const PYODIDE_INDEX_PATH = 'js/libs/pyodide/'
function ensureDirectoryExists(fs: PyodideFS, filePath: string) {
@@ -94,6 +95,7 @@ async function handleRunCode(msg: RunCodeRequest) {
self.postMessage({
type: 'run-code-result',
id: msg.id,
outputs: [],
})
return
}
@@ -102,6 +104,8 @@ async function handleRunCode(msg: RunCodeRequest) {
env: { MPLBACKEND: 'Agg' },
})
const writtenPaths = new Set<string>()
instance.setStdout({
batched: (line: string) => {
self.postMessage({
@@ -124,11 +128,25 @@ async function handleRunCode(msg: RunCodeRequest) {
})
try {
const fs = instance.FS
if (msg.files.length > 0) {
const fs = instance.FS
syncProjectFiles(fs, msg.files)
}
const originalWrite = fs.write as PyodideFS['write']
fs.write = ((...args: Parameters<PyodideFS['write']>) => {
const [stream] = args
// Only surface writes to the synced project workspace, not Pyodide internals.
if (
typeof stream?.path === 'string' &&
stream.path.startsWith(PROJECT_FS_PREFIX)
) {
writtenPaths.add(stream.path)
}
return originalWrite(...args)
}) as PyodideFS['write']
const result = await instance.runPythonAsync(msg.code)
if (result !== undefined) {
self.postMessage({
@@ -148,12 +166,14 @@ async function handleRunCode(msg: RunCodeRequest) {
line: errorMessage,
requestId: msg.id,
})
} finally {
const outputs = [...writtenPaths].sort()
self.postMessage({
type: 'run-code-result',
id: msg.id,
outputs,
})
}
self.postMessage({
type: 'run-code-result',
id: msg.id,
})
}
self.addEventListener('message', async event => {