π 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 π»
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
- Go to Package Manager tab
- Enter package name:
requests - Click Install π₯
- 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 datawordcloud- Create word cloudsgeopandas- Geospatial analysissqlalchemy- Database ORM
3. Working with Files π
Upload a File
- Go to File Manager tab
- Click Upload File
- Select any CSV, JSON, or text file
- 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
- NumPy: numpy.org/learn
- Pandas: pandas.pydata.org/docs/getting_started
- Matplotlib: matplotlib.org/stable/tutorials
- Scikit-learn: scikit-learn.org/stable/tutorial
Example Scripts
Check the examples/ folder for complete scripts:
data_analysis_example.py- Comprehensive data analysisml_example.py- Machine learning demonstrationsvisualization_example.py- Advanced plotting examples
π‘ Pro Tips
- Use variables: Values persist between code executions
- Install first: Install packages before using them
- Check output mode: Switch between stdout, stderr, or both
- Save outputs: Use File Manager to save important results
- Check Session Info: Monitor your session status
- Explore packages: Try installing different packages to see what's available
- Use comments: Add
# commentsto document your code - 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?
- Check the Session Info tab for system status
- Review the full README.md for detailed documentation
- Explore the examples/ folder for inspiration
- Use the Package Manager to install more libraries
Made with β€οΈ using Gradio and HuggingFace Spaces