Skip to main content
run_code() submits a Python cell to the capsule’s persistent Jupyter kernel and returns once execution completes. The return value is an Execution object that contains all output the cell produced — expression values, printed text, and any exception that was raised.

The Execution object

run_code(code) returns an Execution instance with the following fields: result.logs.stdout and result.logs.stderr are lists of string chunks, one per kernel stream message. Join them with "".join(result.logs.stdout) to get the full output.

Error handling

When code raises an exception, run_code() does not raise a Python exception in your calling code. Instead, it populates result.error with an ExecutionError object.
The ExecutionError fields are:
Errors in the executed code populate result.error — they do not raise exceptions in your calling code. Always check result.error when you need to handle failures.

Streaming callbacks

If you want to receive output as it arrives rather than waiting for the cell to finish, pass callback functions to run_code().
The available callbacks are: run_code() still returns the complete Execution object after the cell finishes, even when you use callbacks. The callbacks and the return value contain the same data.