You are reading a legacy (pre v1.0) document.
execCell
method. The method takes a string of code as an argument and returns an object with the results of the execution.
execCell
method also accepts following optional arguments:
kernel id
: The ID of the kernel to execute the code on. If not provided, the default kernel is used. See here for more info on kernels.on stdout
: A callback function to handle standard output messages from the code execution.on_stderr
: A callback function to handle standard error messages from the code execution.on_result
: A callback function to handle the result and display calls of the code execution.
Streaming response
You can use theon_*
callbacks to handle the output of the code execution as it happens. This is useful for long-running code. You can stream the output to the user as it is generated.
Execution object
The object returned by theexec cell
method is little bit more complex, it’s based on Jupyter. Here’s an detailed explanation in the Jupyter documentation.
It contains the following fields:
results
: A list containing result of the cell (interactively interpreted last line) and display calls (e.g. matplotlib plots).logs
: Logs printed to stdout and stderr during execution.error
: An error message, if there was an error during execution of the cell. It works only for Python code, not for system (!
e.g!pip install e2b
) commands.
Result object
This object can be created in two different ways:- Evaluation of the last line: If the last line of the code is an expression, the result is the value of that expression. As you would expect in REPL environments.
- Display calls: Calls to display functions, which can be used to display rich output in the notebook. E.g.
img.show()
,display(img)
, etc.
Text types:
text
: text representation of the resulthtml
: html representation of the resultmarkdown
: markdown representation of the resultlatex
: latex representation of the result
Image types:
png
: “base64 encoded png image”,jpeg
: “base64 encoded jpeg image”,svg
”: “svg image”,
Other types:
json
: “json representation”,javascript
: “javascript representation”,pdf
: “base64 encoded pdf”
If you want to integrate your own display formats or how to implement them for your classes, you can read more in here
Logs object
Logs printed to stdout and stderr during execution. Examples of logs are print statements, warnings, subprocess output, etc. It contains two fields:stdout
: List of strings, each string is a line printed to stdout.stderr
: List of strings, each string is a line printed to stderr.
Error object
An error message, if there was an error during execution of the cell.It works only for Python code, not for system (e.g.
!pip install non_existent_package
) commands. The system commands are executed in a separate process and the output is in stdout/stderr.name
: Name of the error, e.g.NameError
,ValueError
, etc.value
: Value of the error, e.g.name 'non_existent_variable' is not defined
, etc.traceback
: Traceback of the error.