> ## 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.

# Upload Dataset & Analyze

> Upload a CSV dataset and analysis script into a Declaw sandbox, run the analysis, and read back JSON results.

## What You'll Learn

* Writing data files into a sandbox with `sbx.files.write()`
* Writing executable Python scripts into a sandbox
* Running scripts with `sbx.commands.run()`
* Reading generated output files back with `sbx.files.read()`
* End-to-end data pipeline inside an isolated sandbox

## Prerequisites

<Snippet file="snippets/env-setup.mdx" />

<Note>This example is available in Python. TypeScript version coming soon.</Note>

## Code Walkthrough

**Upload the CSV dataset** directly as a string — no disk I/O on the host:

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

sbx = Sandbox.create(template="python", timeout=300)
try:
    csv_data = """name,age,city,salary
Alice,30,New York,85000
Bob,25,San Francisco,92000
Charlie,35,Chicago,78000
Diana,28,Boston,95000
Eve,32,Seattle,88000"""
    sbx.files.write("/tmp/data.csv", csv_data)
    print("Wrote /tmp/data.csv")
```

**Upload the analysis script** as a multiline Python string:

```python theme={null}
    analysis_script = '''
import csv
import json

with open("/tmp/data.csv") as f:
    reader = csv.DictReader(f)
    rows = list(reader)

results = {
    "total_records": len(rows),
    "avg_age": sum(int(r["age"]) for r in rows) / len(rows),
    "avg_salary": sum(int(r["salary"]) for r in rows) / len(rows),
    "cities": list(set(r["city"] for r in rows)),
    "highest_salary": max(rows, key=lambda r: int(r["salary"]))["name"],
}

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

print(json.dumps(results, indent=2))
'''
    sbx.files.write("/tmp/analyze.py", analysis_script)
    print("Wrote /tmp/analyze.py")
```

**Run the analysis** and capture stdout:

```python theme={null}
    result = sbx.commands.run("python3 /tmp/analyze.py")
    print(f"stdout:\n{result.stdout}")
```

**Read back the generated results file:**

```python theme={null}
    results_content = sbx.files.read("/tmp/results.json")
    print(f"results.json:\n{results_content}")
finally:
    sbx.kill()
```

<Tip>
  This pattern works for any file format — JSON, Parquet, images, or binary data. `sbx.files.write()` accepts both string and bytes content.
</Tip>

## Expected Output

```
==================================================
Declaw Upload Dataset & Analyze Example
==================================================

--- Creating Sandbox ---
Sandbox created: sbx_abc123

--- Uploading CSV Dataset ---
Wrote /tmp/data.csv

--- Uploading Analysis Script ---
Wrote /tmp/analyze.py

--- Running Analysis ---
stdout:
{
  "total_records": 5,
  "avg_age": 30.0,
  "avg_salary": 87600.0,
  "cities": ["New York", "San Francisco", "Chicago", "Boston", "Seattle"],
  "highest_salary": "Diana"
}

--- Reading Results File ---
results.json:
{
  "total_records": 5,
  "avg_age": 30.0,
  "avg_salary": 87600.0,
  "cities": ["New York", "San Francisco", "Chicago", "Boston", "Seattle"],
  "highest_salary": "Diana"
}

--- Cleaning Up ---
Sandbox killed.

==================================================
Done!
==================================================
```
