File size: 7,730 Bytes
523f6c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# πŸš€ 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](#1-your-first-code)
2. [Installing Packages](#2-installing-packages)
3. [Working with Files](#3-working-with-files)
4. [Data Visualization](#4-data-visualization)
5. [Examples to Try](#5-examples-to-try)

---

## 1. Your First Code πŸ’»

### Basic Print Statement
```python
print("Hello, World!")
print("Welcome to the Code Interpreter Sandbox!")
print(2 + 2)
```

### Working with Variables
```python
name = "Python"
version = 3.11
print(f"I'm using {name} {version}")
```

### Using Pre-installed Libraries
```python
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
```python
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
```python
# 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
```python
# 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
```python
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
```python
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
```python
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
```python
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
```python
# 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
```python
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
```python
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](https://numpy.org/learn/)
- **Pandas**: [pandas.pydata.org/docs/getting_started](https://pandas.pydata.org/docs/getting_started)
- **Matplotlib**: [matplotlib.org/stable/tutorials](https://matplotlib.org/stable/tutorials)
- **Scikit-learn**: [scikit-learn.org/stable/tutorial](https://scikit-learn.org/stable/tutorial)

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