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

# Data Analysis in a Sandbox

> Upload a CSV dataset into a Declaw sandbox, execute a Python analysis script that computes revenue statistics, and read the JSON results back to the host process.

## What You'll Learn

* How to upload a dataset file with `sbx.files.write()`
* How to write and execute a Python analysis script inside the sandbox
* How to read structured JSON results back using `sbx.files.read()`
* The upload-compute-download pattern for safe, isolated data processing

## Prerequisites

* Declaw running locally or in the cloud (see [Deployment](/deployment/overview))
* `DECLAW_API_KEY` and `DECLAW_DOMAIN` set in your environment

## Code Walkthrough

<Tabs>
  <Tab title="Python">
    ### 1. Define the dataset and analysis script

    Both the CSV data and the analysis script are defined as Python strings and uploaded to the sandbox filesystem:

    ```python theme={null}
    SALES_CSV = """\
    date,product,region,quantity,unit_price
    2024-01-05,Widget A,North,120,9.99
    2024-01-05,Widget B,South,85,14.50
    2024-01-12,Widget A,South,200,9.99
    ...
    """

    ANALYSIS_SCRIPT = """\
    import csv, json
    from collections import defaultdict

    rows = []
    with open("/home/user/data/sales.csv") as f:
        reader = csv.DictReader(f)
        for row in reader:
            row["quantity"] = int(row["quantity"])
            row["unit_price"] = float(row["unit_price"])
            row["revenue"] = row["quantity"] * row["unit_price"]
            rows.append(row)

    total_revenue = sum(r["revenue"] for r in rows)
    rev_by_product = defaultdict(float)
    for r in rows:
        rev_by_product[r["product"]] += r["revenue"]

    top_product = max(rev_by_product, key=rev_by_product.get)

    results = {
        "total_records": len(rows),
        "total_revenue": round(total_revenue, 2),
        "top_product": top_product,
        "revenue_by_product": {k: round(v, 2) for k, v in sorted(rev_by_product.items())},
    }

    with open("/home/user/data/results.json", "w") as f:
        json.dump(results, f, indent=2)

    print("Analysis complete. Results written to /home/user/data/results.json")
    """
    ```

    ### 2. Upload, execute, and read back

    ```python theme={null}
    from declaw import Sandbox
    import json

    sbx = Sandbox.create(template="python", timeout=300)
    try:
        # Upload the dataset and analysis script
        sbx.files.write("/home/user/data/sales.csv", SALES_CSV)
        sbx.files.write("/home/user/data/analyze.py", ANALYSIS_SCRIPT)

        # Run the analysis inside the sandbox
        result = sbx.commands.run("python3 /home/user/data/analyze.py")
        print(result.stdout)  # "Analysis complete. Results written to ..."

        # Read the computed results back to the host
        results_json = sbx.files.read("/home/user/data/results.json")
        data = json.loads(results_json)

        print(f"Total revenue:  ${data['total_revenue']:,.2f}")
        print(f"Top product:    {data['top_product']}")
        for product, rev in data["revenue_by_product"].items():
            print(f"  {product}: ${rev:,.2f}")
    finally:
        sbx.kill()
    ```
  </Tab>

  <Tab title="TypeScript">
    ### 1. Define the dataset and analysis script

    ```typescript theme={null}
    import { Sandbox } from "@declaw/sdk";

    const SALES_CSV = `date,product,region,quantity,unit_price
    2024-01-05,Widget A,North,120,9.99
    2024-01-05,Widget B,South,85,14.50
    ...
    `;

    const ANALYSIS_SCRIPT = `import csv, json
    from collections import defaultdict

    rows = []
    with open("/home/user/data/sales.csv") as f:
        reader = csv.DictReader(f)
        for row in reader:
            row["quantity"] = int(row["quantity"])
            row["unit_price"] = float(row["unit_price"])
            row["revenue"] = row["quantity"] * row["unit_price"]
            rows.append(row)

    total_revenue = sum(r["revenue"] for r in rows)
    rev_by_product = defaultdict(float)
    for r in rows:
        rev_by_product[r["product"]] += r["revenue"]

    results = {
        "total_revenue": round(total_revenue, 2),
        "top_product": max(rev_by_product, key=rev_by_product.get),
        "revenue_by_product": {k: round(v, 2) for k, v in sorted(rev_by_product.items())},
    }

    with open("/home/user/data/results.json", "w") as f:
        json.dump(results, f, indent=2)

    print("Analysis complete.")
    `;
    ```

    ### 2. Upload, execute, and read back

    ```typescript theme={null}
    const sbx = await Sandbox.create({ template: "python", timeout: 300 });
    try {
        // Upload dataset and script
        await sbx.files.write("/home/user/data/sales.csv", SALES_CSV);
        await sbx.files.write("/home/user/data/analyze.py", ANALYSIS_SCRIPT);

        // Run the analysis
        const result = await sbx.commands.run("python3 /home/user/data/analyze.py");
        console.log(result.stdout);

        if (result.exitCode !== 0) {
            console.error(result.stderr);
            return;
        }

        // Read results back
        const resultsJson = await sbx.files.read("/home/user/data/results.json");
        const data = JSON.parse(resultsJson);

        console.log(`Total revenue:  $${data.total_revenue.toLocaleString()}`);
        console.log(`Top product:    ${data.top_product}`);
        for (const [product, rev] of Object.entries(data.revenue_by_product)) {
            console.log(`  ${product}: $${(rev as number).toLocaleString()}`);
        }
    } finally {
        await sbx.kill();
    }
    ```
  </Tab>
</Tabs>

## Expected Output

```
--- Creating Sandbox ---
Sandbox created: sbx-abc123

--- Uploading Dataset ---
  Uploaded: sales.csv

--- Running Analysis ---
  stdout: Analysis complete. Results written to /home/user/data/results.json

--- Summary ---
  Total records:     15
  Total revenue:     $27,897.40
  Total quantity:    1,640
  Avg order value:   $1,859.83
  Top product:       Widget A

  Revenue by product:
    Widget A: $11,680.30
    Widget B: $8,265.00
    Widget C: $7,952.10

  Revenue by region:
    East:  $7,012.20
    North: $9,865.50
    South: $11,019.70
```

## Pattern Notes

**Why run the analysis inside the sandbox instead of locally?**

* The analysis script could be untrusted (for example, user-submitted code in a data pipeline)
* The dataset may contain sensitive data that should not be processed on the host
* Sandbox isolation ensures the script cannot access host files, environment variables, or network resources

**Scaling the pattern:**

* Use `sbx.files.write_files()` to upload multiple files in a single API call
* Use `sbx.commands.run_stream()` for long-running analyses where you want incremental output
* Add `allow_internet_access=False` to block all outbound network access during analysis
