0.5.4

Run Python Code

Execute Python code securely in isolated sandboxes.

sandbox.run( code: str, timeout: int = 10000, language: str = "python" ) -> RunResult

Parameters

codestringrequired

Python code to execute. Can be a single line or multi-line script.

timeoutint

Timeout in milliseconds. Defaults to 10000ms (10 seconds).

languagestring

Programming language to use. Currently only "python" is supported. Default value is "python".

Returns

stdoutstr

Standard output from the code execution.

stderrstr

Standard error from the code execution.

returncodeint

Exit code from the execution (0 = success, non-zero = error).

codestr

The original code that was executed.

languagestr

The language that was executed (currently only "python").

Examples

Basic Example

from concave import Sandbox sbx = Sandbox.create() result = sbx.run("print('Hello from Python!')") print(result.stdout) # Hello from Python! sbx.delete()

Multi-Line Code

from concave import Sandbox sbx = Sandbox.create() code = """ def fibonacci(n): if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2) print(fibonacci(10)) """ result = sbx.run(code) print(result.stdout) # 55 sbx.delete()

Error Handling

from concave import Sandbox sbx = Sandbox.create() result = sbx.run("print(1/0)") if result.returncode != 0: print(f"Error: {result.stderr}") else: print(result.stdout) sbx.delete()

Exceptions

SandboxValidationErrorException

Raised when the code is empty or language is unsupported.

SandboxNotFoundErrorException

Raised when the sandbox no longer exists.

SandboxTimeoutErrorException

Raised when code execution exceeds the timeout duration.

SandboxConnectionErrorException

Raised when unable to connect to the sandbox service.