frontend / features/CodeRunner/PyodideWorkerClient / PyodideWorkerClient
Class: PyodideWorkerClient
Defined in: features/CodeRunner/PyodideWorkerClient.ts:34
Client wrapper for communication with Pyodide Web Worker.
Every command to worker is sent as message with unique numeric id. Commands which expect answer (like runCode) wait for response with same id and resolve promise. Commands which do not expect answer (like stopCode) just send message and return nothing.
Worker can also send unsolicited events without id — for example stdout chunks or debug pause signal. Those are routed to callbacks which are passed in constructor.
Constructors
Constructor
new PyodideWorkerClient(
callbacks):PyodideWorkerClient
Defined in: features/CodeRunner/PyodideWorkerClient.ts:44
Parameters
callbacks
Returns
PyodideWorkerClient
Accessors
isReady
Get Signature
get isReady():
boolean
Defined in: features/CodeRunner/PyodideWorkerClient.ts:125
Tells if initialization is already complete.
Returns false until worker sends InitComplete event. Unlike whenReady, this getter is synchronous and can be checked inside render or event handlers without waiting.
Returns
boolean
Methods
debugCode()
debugCode(
code,breakpoints,inputFilenames?,stdinText?):Promise<unknown>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:207
Runs Python code in debug mode with given breakpoints.
Code is transformed before execution: each top-level statement gets await _check_breakpoint(N) in front of it, where N is 1-based line number. When execution hits line from breakpoints list, worker pauses and sends OnDebugFileCreated event with current variables snapshot.
Promise resolves only when debugging is fully done (code finished, user stopped it or exception was raised).
Parameters
code
string
Python code to execute.
breakpoints
number[]
1-based line numbers where execution should pause.
inputFilenames?
string[] = []
Names of files to keep in worker filesystem.
stdinText?
string = ''
Text which will be used as stdin source.
Returns
Promise<unknown>
debugContinue()
debugContinue():
void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:225
Tells worker to continue execution until next breakpoint.
Returns
void
debugStep()
debugStep():
void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:232
Tells worker to continue execution but pause on very next line.
Returns
void
debugStop()
debugStop():
void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:242
Stops debug session from inside paused state.
Unlike stopCode, this works while debug code is waiting on future — it raises CancelledError inside paused coroutine.
Returns
void
dispose()
dispose():
void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:279
Terminates worker and clears all pending promises.
Pending promises are not rejected — they just stay unresolved forever. This is fine for unmount, because no one waits for them anymore.
Returns
void
listOutputFiles()
listOutputFiles():
Promise<string[]>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:261
Returns list of files which exist in worker filesystem right now.
Includes internal files (like __debug_data.json), but not input files which are just uploaded by user. Filtering of input files happens on consumer side, see useInputOutputFiles.
Returns
Promise<string[]>
loadInputFile()
loadInputFile(
filename,data):Promise<void>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:169
Uploads file into worker filesystem. File becomes available for Python code by given name.
If file with same name already exists, it is silently overwritten. This behavior can change in future, see IN-04 in manual test checklist.
Parameters
filename
string
data
Uint8Array
Returns
Promise<void>
readDebugSnapshot()
readDebugSnapshot():
Promise<DebugSnapshot>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:218
Reads last debug snapshot from worker filesystem.
Should be called only after OnDebugFileCreated event, otherwise returns data from previous pause or throws if there was no pause yet.
Returns
Promise<DebugSnapshot>
readOutputFile()
readOutputFile(
filename):Promise<string>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:269
Reads file content as UTF-8 string. Throws if file does not exist or is binary.
Parameters
filename
string
Returns
Promise<string>
removeInputFile()
removeInputFile(
filename):void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:179
Removes file from worker filesystem. Does not throw if file is missing — worker just logs error into console.
Parameters
filename
string
Returns
void
runCode()
runCode(
code,inputFilenames?,stdinText?):Promise<unknown>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:146
Runs Python code in worker and waits until it finishes.
Code is transformed before execution: each top-level statement gets _check_stop_run_code() call in front of it, so user can stop long loops by pressing stop button. stdin is installed as io.StringIO with given text.
Parameters
code
string
Python code to execute.
inputFilenames?
string[] = []
Names of files which should stay in worker filesystem. All other files are removed before run — this gives clean state between runs.
stdinText?
string = ''
Text which will be used as stdin source. Each line becomes one input() call.
Returns
Promise<unknown>
Promise which resolves with result of runPythonAsync or rejects if Python code raised exception.
saveOutputFilesZip()
saveOutputFilesZip():
void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:250
Asks worker to pack all output files into zip archive. When archive is ready, onOutputFilesZip callback is called with archive bytes.
Returns
void
setDebugPausedHandler()
setDebugPausedHandler(
handler):void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:187
Replaces debug pause callback. Useful when hook wants to change handler without recreating whole client.
Parameters
handler
() => void
Returns
void
stopCode()
stopCode():
void
Defined in: features/CodeRunner/PyodideWorkerClient.ts:158
Sends stop signal to worker. Does not wait for answer.
Works only for regular code execution — in debug mode use debugStop instead, because debug code waits on future and stop flag would not wake it up.
Returns
void
whenReady()
whenReady():
Promise<void>
Defined in: features/CodeRunner/PyodideWorkerClient.ts:114
Returns promise which resolves when worker finishes initialization.
Initialization includes loading Pyodide, installing packages (requests, pandas, lxml, micropip) and running init script. Usually takes several seconds on first load, so UI should show splash screen until this promise resolves.
Returns
Promise<void>