> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ns.rocks/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Interpreter

> Run stateful notebook-style code, install a package, and render a plot.

The `code-interpreter` template runs a Jupyter kernel inside the machine:
variables persist between calls, packages install at runtime, and rich
results like plots come back as data.

## Setup

```bash theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
uv pip install "nullspace-sdk==1.0.0"
export NULLSPACE_API_KEY=ns_live_...
export NULLSPACE_API_URL=https://api.your-nullspace-domain
```

## Python

```python interpreter_example.py theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
from nullspace import Machine

with Machine.create(template="code-interpreter", timeout=120) as machine:
    # State persists across run_code calls, like notebook cells.
    machine.run_code("value = 21")
    result = machine.run_code("value * 2")
    print("stateful result:", result.results[0].text)

    # Install a package into the live kernel.
    machine.install_packages(["humanize==4.9.0"])
    result = machine.run_code("import humanize; print(humanize.intword(1234567))")
    print("package output:", "".join(result.logs.stdout).strip())

    # Plots come back as PNG data on the result.
    plot = machine.run_code("""
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
""")
    print("plot rendered:", any(r.png for r in plot.results))
```

## Expected output

```text theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
stateful result: 42
package output: 1.2 million
plot rendered: True
```

If a call fails inside the kernel, the returned result carries the error
instead of raising: check `result.error` for the exception name, value, and
traceback.

Related: [Code Interpreter overview](../code-interpreter/overview) ·
[SDK guide](../guides/python-sdk/code-interpreter) ·
[Charts](../code-interpreter/charts-overview)
