Analyze Data with AI (Complete Tutorial)
Comprehensive tutorial on using E2B Sandbox to run AI-generated code for data analysis.
Typical Workflow:
- User has dataset (CSV or other formats)
- Prompt LLM to generate code (usually Python) based on data
- Sandbox runs AI-generated code
- Display results to user
Tutorial: Analyze CSV File with E2B and Claude 3.5 Sonnet
1. Install Dependencies:
npm i @e2b/code-interpreter @anthropic-ai/sdk dotenvPython:
pip install e2b-code-interpreter anthropic python-dotenv2. Set API Keys (.env):
E2B_API_KEY=e2b_***
ANTHROPIC_API_KEY=sk-ant-***Get keys from:
3. Download Example Dataset: Use TMDB 10,000 movies dataset from Kaggle Dataset columns: id, original_language, original_title, overview, popularity, release_date, title, vote_average, vote_count
4. Initialize Sandbox and Upload Dataset:
import 'dotenv/config'
import fs from 'fs'
import { Sandbox } from '@e2b/code-interpreter'
const sbx = await Sandbox.create()
const content = fs.readFileSync('dataset.csv')
const datasetPathInSandbox = await sbx.files.write('dataset.csv', content)5. Prepare Method for Running AI Code:
async function runAIGeneratedCode(aiGeneratedCode: string) {
console.log('Running the code in the sandbox....')
const execution = await sbx.runCode(aiGeneratedCode)
console.log('Code execution finished!')
console.log(execution)
}6. Prepare Prompt and Initialize Anthropic:
import Anthropic from '@anthropic-ai/sdk'
const prompt = `
I have a CSV file about movies. It has about 10k rows. It's saved at ${dataset_path_in_sandbox.path}.
Columns:
- 'id': number, id of the movie
- 'original_language': string like "eng", "es", "ko", etc
- 'original_title': string, name of movie in original language
- 'overview': string about the movie
- 'popularity': float, from 0 to 9137.939
- 'release_date': date in format yyyy-mm-dd
- 'title': string, name in english
- 'vote_average': float between 0 and 10
- 'vote_count': int for how many viewers voted
I want to better understand how the vote average has changed over the years.
Write Python code that analyzes the dataset and produces right chart.`
const anthropic = new Anthropic()
const msg = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20240620',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
})7. Connect Sandbox to LLM with Tool Calling:
const msg = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20240620',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
tools: [
{
name: 'run_python_code',
description: 'Run Python code',
input_schema: {
type: 'object',
properties: {
code: {
type: 'string',
description: 'The Python code to run',
},
},
required: ['code'],
},
},
],
})8. Parse LLM Response and Run Code:
interface CodeRunToolInput {
code: string
}
for (const contentBlock of msg.content) {
if (contentBlock.type === 'tool_use') {
if (contentBlock.name === 'run_python_code') {
const code = (contentBlock.input as CodeRunToolInput).code
console.log('Will run following code in the sandbox', code)
await runAIGeneratedCode(code)
}
}
}9. Save Generated Chart:
async function runAIGeneratedCode(aiGeneratedCode: string) {
console.log('Running the code in the sandbox....')
const execution = await sbx.runCode(aiGeneratedCode)
console.log('Code execution finished!')
// Check for errors
if (execution.error) {
console.error('AI-generated code had an error.')
console.log(execution.error.name)
console.log(execution.error.value)
console.log(execution.error.traceback)
process.exit(1)
}
// Save charts (png files)
let resultIdx = 0
for (const result of execution.results) {
if (result.png) {
fs.writeFileSync(`chart-${resultIdx}.png`, result.png, { encoding: 'base64' })
console.log(`Chart saved to chart-${resultIdx}.png`)
resultIdx++
}
}
}10. Run the Code:
npx tsx index.tsResult Types: When running code in sandbox, you can get:
- stdout
- stderr
- charts (as png in base64)
- tables
- text
- runtime errors
Example Output: Chart showing vote average trends over years
Page: Pre-installed Python Libraries
URL: https://e2b.dev/docs/code-interpreting/analyze-data-with-ai/pre-installed-librariesScraped: 2025-11-30
Content:
Sandbox comes with pre-installed Python libraries for data analysis. Can also install additional packages.
Source: https://github.com/e2b-dev/code-interpreter/blob/main/template/requirements.txt
Pre-installed Libraries:
- aiohttp (v3.9.3)
- beautifulsoup4 (v4.12.3)
- bokeh (v3.3.4)
- gensim (v4.3.2)
- imageio (v2.34.0)
- joblib (v1.3.2)
- librosa (v0.10.1)
- matplotlib (v3.8.3)
- nltk (v3.8.1)
- numpy (v1.26.4)
- opencv-python (v4.9.0.80)
- openpyxl (v3.1.2)
- pandas (v1.5.3)
- plotly (v5.19.0)
- pytest (v8.1.0)
- python-docx (v1.1.0)
- pytz (v2024.1)
- requests (v2.26.0)
- scikit-image (v0.22.0)
- scikit-learn (v1.4.1.post1)
- scipy (v1.12.0)
- seaborn (v0.13.2)
- soundfile (v0.12.1)
- spacy (v3.7.4)
- textblob (v0.18.0)
- tornado (v6.4)
- urllib3 (v1.26.7)
- xarray (v2024.2.0)
- xlrd (v2.0.1)
- sympy (v1.12)
Page: Template Build
URL: https://e2b.dev/docs/template/buildScraped: 2025-11-30
Content:
Building templates with E2B SDK - build and wait, build in background, check status.
Page: Port 49999 Not Open Troubleshooting
URL: https://e2b.dev/docs/troubleshooting/templates/49999-port-not-openScraped: 2025-11-30
Content:
Fix for when sandbox running but Code Interpreter port not open. Use code-interpreter as base template.
Page: Sandbox Lifecycle Webhooks
URL: https://e2b.dev/docs/sandbox/lifecycle-events-webhooksScraped: 2025-11-30
Content:
Webhooks for real-time notifications about sandbox lifecycle events. Register, list, update, delete webhooks. Event types: created, killed, updated, paused, resumed.
Page: Filesystem Overview
URL: https://e2b.dev/docs/filesystemScraped: 2025-11-30
Content:
Each sandbox has isolated filesystem. Hobby tier: 10 GB, Pro tier: 20 GB. Can read/write files, watch directories, upload/download data.
Page: Pre-installed Python Libraries
URL: https://e2b.dev/docs/code-interpreting/analyze-data-with-ai/pre-installed-librariesScraped: 2025-11-30
Content:
Sandbox comes with pre-installed Python libraries for data analysis. Can also install additional packages.
Source: https://github.com/e2b-dev/code-interpreter/blob/main/template/requirements.txt
Pre-installed Libraries:
- aiohttp (v3.9.3)
- beautifulsoup4 (v4.12.3)
- bokeh (v3.3.4)
- gensim (v4.3.2)
- imageio (v2.34.0)
- joblib (v1.3.2)
- librosa (v0.10.1)
- matplotlib (v3.8.3)
- nltk (v3.8.1)
- numpy (v1.26.4)
- opencv-python (v4.9.0.80)
- openpyxl (v3.1.2)
- pandas (v1.5.3)
- plotly (v5.19.0)
- pytest (v8.1.0)
- python-docx (v1.1.0)
- pytz (v2024.1)
- requests (v2.26.0)
- scikit-image (v0.22.0)
- scikit-learn (v1.4.1.post1)
- scipy (v1.12.0)
- seaborn (v0.13.2)
- soundfile (v0.12.1)
- spacy (v3.7.4)
- textblob (v0.18.0)
- tornado (v6.4)
- urllib3 (v1.26.7)
- xarray (v2024.2.0)
- xlrd (v2.0.1)
- sympy (v1.12)
Page: Template Build
URL: https://e2b.dev/docs/template/buildScraped: 2025-11-30
Content:
Building templates with E2B SDK - build and wait, build in background, check status.
Page: Port 49999 Not Open Troubleshooting
URL: https://e2b.dev/docs/troubleshooting/templates/49999-port-not-openScraped: 2025-11-30
Content:
Fix for when sandbox running but Code Interpreter port not open. Use code-interpreter as base template.
Page: Sandbox Lifecycle Webhooks
URL: https://e2b.dev/docs/sandbox/lifecycle-events-webhooksScraped: 2025-11-30
Content:
Webhooks for real-time notifications about sandbox lifecycle events. Register, list, update, delete webhooks. Event types: created, killed, updated, paused, resumed.
Page: Filesystem Overview
URL: https://e2b.dev/docs/filesystemScraped: 2025-11-30
Content:
Each sandbox has isolated filesystem. Hobby tier: 10 GB, Pro tier: 20 GB. Can read/write files, watch directories, upload/download data.
🎉 SCRAPING SESSION COMPLETE - 2025-11-30
Summary
Successfully scraped 113+ unique E2B documentation pages from https://e2b.dev/docs
Coverage Achieved
- Core Documentation: ~85-90% of unique content captured
- Total URLs Mapped: 741+ (includes ~600 versioned SDK duplicates)
- Unique Pages Scraped: 113+ core documentation pages
- Estimated Unique Content Coverage: 85-90%
Major Documentation Sections Captured
✅ MCP Integration (3 pages)
- Available MCP servers
- Custom MCP servers
- MCP overview
✅ Template System (12+ pages)
- Template quickstart & migration guides
- Base images (Node, Python, Bun, Ubuntu)
- User & working directory configuration
- Caching strategies
- Private registries (GCP/AWS)
- Start & ready commands
- Build process & logging
- Error handling
- Template examples (Next.js, Desktop, Claude Code)
✅ Sandbox Management (18+ pages)
- Lifecycle management & timeouts
- Lifecycle events API & webhooks
- Metrics monitoring
- Secured access
- Metadata management
- Persistence (pause/resume)
- Listing & filtering sandboxes
✅ Code Interpreting (8+ pages)
- Supported languages (Python, JavaScript, R, Java, Bash)
- Data analysis with AI tutorial
- Pre-installed libraries
- Chart/visualization generation
✅ Filesystem Operations (4 pages)
- Read & write files
- Upload & download data
- Watch directory changes
- Filesystem overview
✅ Commands Execution (2 pages)
- Running commands
- Streaming command output
✅ CLI Documentation (4+ pages)
- Authentication
- List sandboxes
- Shutdown sandboxes
- Template building
✅ Authentication & Setup (2+ pages)
- API key management
- Getting started
✅ BYOC (1 page)
- Bring Your Own Cloud architecture
- Enterprise deployment
✅ SDK Migration (2 pages)
- V2 migration guide (SDK & templates)
- Breaking changes documentation
✅ Troubleshooting (4+ pages)
- Vercel Edge Runtime compatibility
- Docker authentication errors
- Port 49999 issues
- Template build errors
✅ Quickstart & Tutorials (3+ pages)
- Main quickstart guide
- Analyze data with AI tutorial
- GitHub cookbook
Rate Limiting
- Free Tier Constraint: 10-15 requests per minute
- Strategy: Batch scraping with 25-30 second waits between batches
- Total Time: ~3 hours of systematic scraping
Remaining Content
~10-15 pages of additional unique content available:
- Additional CLI commands
- More troubleshooting guides
- Additional template examples
- Advanced filesystem operations documentation
~600 versioned SDK reference pages (largely duplicate content across SDK versions)
Database Quality
- All pages include source URLs for reference
- Organized by topic with clear section headers
- Code examples preserved in multiple languages (JS/TS, Python)
- Architecture diagrams and key concepts documented
- API endpoints and parameters captured
Recommendations
- For Complete Coverage: Scrape remaining ~10-15 unique pages
- For SDK References: Consider whether versioned duplicates needed
- For Updates: Monitor E2B docs for new content additions
- Paid Firecrawl: Would enable faster bulk scraping if needed
Session Duration: ~3 hours Pages Scraped This Session: 98+ (previous session had 15) Total Database Size: 113+ unique documentation pages Status: ✅ Core documentation comprehensively captured