execute_code

execute_code(
    code: str | None = None,
    timeout: int | None = None,
    filename: str | None = None,
    work_dir: str | None = None,
    use_docker: list[str] | str | bool = <object object>,
    lang: str | None = 'python'
) -> tuple[int, str, str | None]

Execute code in a docker container. This function is not tested on MacOS.

Parameters:
NameDescription
codeThe code to execute.

If None, the code from the file specified by filename will be executed.

Either code or filename must be provided.

Type: str | None

Default: None
timeoutThe maximum execution time in seconds.

If None, a default timeout will be used.

The default timeout is 600 seconds.

On Windows, the timeout is not enforced when use_docker=False.

Type: int | None

Default: None
filenameThe file name to save the code or where the code is stored when code is None.

If None, a file with a randomly generated name will be created.

The randomly generated file will be deleted after execution.

The file name must be a relative path.

Relative paths are relative to the working directory.

Type: str | None

Default: None
work_dirThe working directory for the code execution.

If None, a default working directory will be used.

The default working directory is the “extensions” directory under “path_to_autogen”.

Type: str | None

Default: None
use_dockerThe docker image to use for code execution.

Default is True, which means the code will be executed in a docker container.

A default list of images will be used.

If a list or a str of image name(s) is provided, the code will be executed in a docker container with the first image successfully pulled.

If False, the code will be executed in the current environment.

Expected behaviour: - If use_docker is not set (i.e.

left default to True) or is explicitly set to True and the docker package is available, the code will run in a Docker container.

- If use_docker is not set (i.e.

left default to True) or is explicitly set to True but the Docker package is missing or docker isn’t running, an error will be raised.

- If use_docker is explicitly set to False, the code will run natively.

If the code is executed in the current environment, the code must be trusted.

Type: list[str] | str | bool

Default: <object object>
langThe language of the code.

Default is “python”.

Type: str | None

Default: ‘python’
Returns:
TypeDescription
tuple[int, str, str | None]int: 0 if the code executes successfully. str: The error message if the code fails to execute; the stdout otherwise. image: The docker image name after container run when docker is used.