code-interpreter-sandbox / QUICKSTART.md
likhonsheikh's picture
Initial commit: Advanced Code Interpreter Sandbox
523f6c3 verified

πŸš€ Quick Start Guide

Welcome to the Advanced Code Interpreter Sandbox! This guide will help you get started in just a few minutes.

πŸ“‹ Table of Contents

  1. Your First Code
  2. Installing Packages
  3. Working with Files
  4. Data Visualization
  5. Examples to Try

1. Your First Code πŸ’»

Basic Print Statement

print("Hello, World!")
print("Welcome to the Code Interpreter Sandbox!")
print(2 + 2)

Working with Variables

name = "Python"
version = 3.11
print(f"I'm using {name} {version}")

Using Pre-installed Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create a simple array
arr = np.array([1, 2, 3, 4, 5])
print(f"Array: {arr}")
print(f"Mean: {np.mean(arr)}")

Try it: Copy any of these examples into the Code Executor tab and click "Run Code" ▢️


2. Installing Packages πŸ“¦

Method 1: Using Package Manager Tab

  1. Go to Package Manager tab
  2. Enter package name: requests
  3. Click Install πŸ“₯
  4. Wait for success message

Method 2: Install via Code

import subprocess
import sys

# Install a package
subprocess.run([sys.executable, "-m", "pip", "install", "faker"])
print("Package installed!")

Popular Packages to Try

  • faker - Generate fake data
  • wordcloud - Create word clouds
  • geopandas - Geospatial analysis
  • sqlalchemy - Database ORM

3. Working with Files πŸ“

Upload a File

  1. Go to File Manager tab
  2. Click Upload File
  3. Select any CSV, JSON, or text file
  4. Click Upload πŸ“€

Access Your Files in Code

# List all files
import os
print("Files in workspace:", os.listdir('.'))

# Read a file
with open('your_file.txt', 'r') as f:
    content = f.read()
    print(content)

Create and Save Files

# Create a new file
with open('my_data.txt', 'w') as f:
    f.write("This is my data\n")
    f.write("Line 2\n")
    f.write("Line 3\n")

print("File created successfully!")

4. Data Visualization πŸ“Š

Basic Matplotlib Plot

import matplotlib.pyplot as plt
import numpy as np

# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create plot
plt.figure(figsize=(10, 6))
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()

Interactive Plot with Plotly

import plotly.express as px
import numpy as np

# Create scatter plot
x = np.random.randn(500)
y = np.random.randn(500)
fig = px.scatter(x=x, y=y, title='Random Scatter Plot')
fig.show()

Data Analysis Example

import pandas as pd
import numpy as np

# Create sample data
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
    'Sales': [100, 120, 150, 110, 200]
}
df = pd.DataFrame(data)

# Analyze
print(df)
print(f"\nAverage sales: {df['Sales'].mean():.2f}")
print(f"Total sales: {df['Sales'].sum()}")

# Plot
import matplotlib.pyplot as plt
plt.bar(df['Month'], df['Sales'])
plt.title('Monthly Sales')
plt.show()

5. Examples to Try 🎯

Example 1: Data Analysis

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
np.random.seed(42)
dates = pd.date_range('2023-01-01', periods=30, freq='D')
values = np.random.randn(30).cumsum()

# Create DataFrame
df = pd.DataFrame({'date': dates, 'value': values})

# Display
print(df.head(10))
print(f"\nStatistics:\n{df['value'].describe()}")

# Plot
plt.figure(figsize=(12, 6))
plt.plot(df['date'], df['value'])
plt.title('Time Series Data')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Example 2: Web Scraping

# Install requests and beautifulsoup4 first via Package Manager
import requests
from bs4 import BeautifulSoup

# Get a webpage
url = "https://httpbin.org/html"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# Extract title
title = soup.find('title')
print(f"Page title: {title.text if title else 'No title found'}")

# Get headings
headings = soup.find_all(['h1', 'h2', 'h3'])
print(f"\nFound {len(headings)} headings:")
for h in headings:
    print(f"  - {h.text}")

Example 3: Machine Learning

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate data
X, y = make_classification(n_samples=1000, n_features=4, n_classes=2, random_state=42)

# Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Predict
accuracy = model.score(X_test, y_test)
print(f"Model accuracy: {accuracy:.4f}")

# Feature importance
importance = model.feature_importances_
print(f"\nFeature importance: {importance}")

Example 4: API Integration

import requests
import json

# Call an API
response = requests.get("https://api.github.com/users/octocat")
data = response.json()

# Display key info
print(f"User: {data.get('login', 'N/A')}")
print(f"Name: {data.get('name', 'N/A')}")
print(f"Public repos: {data.get('public_repos', 0)}")
print(f"Followers: {data.get('followers', 0)}")

πŸŽ“ Learning Resources

Tutorials

Example Scripts

Check the examples/ folder for complete scripts:

  • data_analysis_example.py - Comprehensive data analysis
  • ml_example.py - Machine learning demonstrations
  • visualization_example.py - Advanced plotting examples

πŸ’‘ Pro Tips

  1. Use variables: Values persist between code executions
  2. Install first: Install packages before using them
  3. Check output mode: Switch between stdout, stderr, or both
  4. Save outputs: Use File Manager to save important results
  5. Check Session Info: Monitor your session status
  6. Explore packages: Try installing different packages to see what's available
  7. Use comments: Add # comments to document your code
  8. Handle errors: Use try/except blocks for robust code

πŸ†˜ Troubleshooting

Package Installation Fails

  • Check package name (must match PyPI)
  • Some packages need system dependencies
  • Try installing one at a time

Code Doesn't Run

  • Check for syntax errors
  • Verify variable names
  • Ensure packages are installed
  • Check output mode setting

Can't Upload File

  • File size must be < 100MB
  • Supported formats: CSV, JSON, TXT, etc.

Performance Issues

  • Reduce data size
  • Avoid infinite loops
  • Clear variables you don't need
  • Restart session if needed

πŸŽ‰ You're Ready!

You now know the basics of the Code Interpreter Sandbox. Start experimenting with your own ideas!

What to Try Next:

  • Analyze your own data file
  • Create a custom visualization
  • Build a machine learning model
  • Scrape data from the web
  • Create a web dashboard

Happy Coding! πŸš€


πŸ“ž Need Help?

  1. Check the Session Info tab for system status
  2. Review the full README.md for detailed documentation
  3. Explore the examples/ folder for inspiration
  4. Use the Package Manager to install more libraries

Made with ❀️ using Gradio and HuggingFace Spaces