Skip to main content
The python template ships Python 3.10 with pip, venv, and requests, httpx, pydantic, numpy, and pandas already installed. Pick it for data-processing scripts, REST clients, or anything that needs pandas / numpy without a custom build.

What you’ll learn

  • Picking template="python" to skip a pip install pandas step
  • Writing input data into the sandbox with sbx.files.write
  • Running a Python analysis script and reading JSON back

Prerequisites

export DECLAW_API_KEY="your-api-key"
export DECLAW_DOMAIN="your-declaw-instance.example.com:8080"

Code

import json
import textwrap

from declaw import Sandbox


CSV = textwrap.dedent("""\
    region,product,units,revenue
    NA,widget,120,2400
    EU,widget,80,1600
    NA,gizmo,30,1500
    EU,gizmo,55,2750
    APAC,widget,40,800
    APAC,gizmo,70,3500
""")

ANALYSIS = textwrap.dedent("""
    import json
    import pandas as pd

    df = pd.read_csv("/tmp/sales.csv", keep_default_na=False)
    by_region = (
        df.groupby("region")[["units", "revenue"]]
          .sum()
          .reset_index()
          .to_dict(orient="records")
    )
    top_product = (
        df.groupby("product")["revenue"]
          .sum()
          .idxmax()
    )
    out = {
        "by_region": by_region,
        "top_product": top_product,
        "total_revenue": int(df["revenue"].sum()),
        "pandas_version": pd.__version__,
    }
    with open("/tmp/result.json", "w") as f:
        json.dump(out, f)
""")


def main() -> None:
    sbx = Sandbox.create(template="python", timeout=120)
    try:
        sbx.files.write("/tmp/sales.csv", CSV)
        sbx.files.write("/tmp/analyze.py", ANALYSIS)

        r = sbx.commands.run("python3 /tmp/analyze.py")
        if r.exit_code != 0:
            print("analysis failed:", r.stderr)
            return

        result = json.loads(sbx.files.read("/tmp/result.json"))
        print(json.dumps(result, indent=2))
    finally:
        sbx.kill()


if __name__ == "__main__":
    main()

Expected output

{
  "by_region": [
    {"region": "APAC", "units": 110, "revenue": 4300},
    {"region": "EU",   "units": 135, "revenue": 4350},
    {"region": "NA",   "units": 150, "revenue": 3900}
  ],
  "top_product": "gizmo",
  "total_revenue": 12550,
  "pandas_version": "2.3.3"
}
The python template intentionally does not include heavy ML packages (torch, transformers, scipy, scikit-learn). If you need those, build a custom template — see Build a custom template.