Analyze a CSV file
Upload a data file, run pandas and download the chart.
| Template | base (2 vCPU, 512 MiB, $0.039 per hour), or your own |
|---|---|
| Run time | About 18 seconds, 8 seconds with a template |
| Cost of a run | Less than $0.001. A template build is free. |
| Cleanup | The finally block kills the sandbox after the download. |
1The data
Save as sales.csv next to the script:
sales.csv
month,region,revenue 2026-01,north,120 2026-01,south,95 2026-02,north,132 2026-02,south,101 2026-03,north,128 2026-03,south,117 2026-04,north,145 2026-04,south,123
2The code
analyze.py
from fdtn import Sandbox ANALYSIS = """ import pandas as pd import matplotlib matplotlib.use("Agg") # no screen in a sandbox df = pd.read_csv("/home/user/sales.csv") table = df.pivot(index="month", columns="region", values="revenue") print(table.to_string()) print("total by region:", df.groupby("region")["revenue"].sum().to_dict()) ax = table.plot(marker="o", title="Revenue by month") ax.figure.savefig("/home/user/chart.png", dpi=120) """ sbx = Sandbox.create( "base", timeout=600, # the sandbox lives 10 minutes at most metadata={"example": "data"}, ) try: # 1. Install the libraries. About 10 seconds. sbx.commands.run("pip install --quiet pandas matplotlib", timeout=300) # 2. Upload the data and the analysis. with open("sales.csv", "rb") as f: sbx.files.write("/home/user/sales.csv", f) sbx.files.write("/home/user/analysis.py", ANALYSIS) # 3. Run it and print the result. print(sbx.commands.run("python3 /home/user/analysis.py", timeout=120).stdout) # 4. Download the chart. png = sbx.files.read("/home/user/chart.png", format="bytes") with open("chart.png", "wb") as f: f.write(png) print(f"chart.png: {len(png)} bytes") finally: sbx.kill()
import { openAsBlob } from 'node:fs' import { writeFile } from 'node:fs/promises' import { Sandbox } from '@fdtn/sdk' const ANALYSIS = ` import pandas as pd import matplotlib matplotlib.use("Agg") # no screen in a sandbox df = pd.read_csv("/home/user/sales.csv") table = df.pivot(index="month", columns="region", values="revenue") print(table.to_string()) print("total by region:", df.groupby("region")["revenue"].sum().to_dict()) ax = table.plot(marker="o", title="Revenue by month") ax.figure.savefig("/home/user/chart.png", dpi=120) ` const sbx = await Sandbox.create('base', { timeoutMs: 600_000, // the sandbox lives 10 minutes at most metadata: { example: 'data' }, }) try { // 1. Install the libraries. About 10 seconds. await sbx.commands.run('pip install --quiet pandas matplotlib', { timeoutMs: 300_000 }) // 2. Upload the data and the analysis. await sbx.files.write('/home/user/sales.csv', await openAsBlob('sales.csv')) await sbx.files.write('/home/user/analysis.py', ANALYSIS) // 3. Run it and print the result. console.log((await sbx.commands.run('python3 /home/user/analysis.py', { timeoutMs: 120_000 })).stdout) // 4. Download the chart. const png = await sbx.files.read('/home/user/chart.png', { format: 'bytes' }) await writeFile('chart.png', png) console.log(`chart.png: ${png.length} bytes`) } finally { await sbx.kill() }
3Run it
chart.png (a 768 × 576 line chart) is now in your folder.
$ python analyze.py # or: node analyze.mjs region north south month 2026-01 120 95 2026-02 132 101 2026-03 128 117 2026-04 145 123 total by region: {'north': 525, 'south': 436} chart.png: 35851 bytes
4Skip the install with a template
Build the packages into a template once to save about 10 seconds per run.
- Save this
Dockerfilein an empty folder:
Dockerfile
FROM python:3.12-slim-bookworm RUN pip install --no-cache-dir pandas matplotlib WORKDIR /home/user
- Build it (1 minute 18 seconds in our test):
$ fdtn template create my-data
...
Template my-data is ready.- In the script, change
"base"to"my-data"and remove thepip installline.
The template has 2 vCPU and 1 GiB by default: $0.042 per hour. Delete it when done: fdtn template delete my-data --yes. Templates
Notes
- The upload streams the open file, so a large file does not fill your memory.
- Read binary files with
format="bytes"or{ format: 'bytes' }. A text read breaks the PNG. matplotlib.use("Agg")draws without a screen.