forked from e2b-dev/E2B
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analysis_example.py
More file actions
57 lines (42 loc) · 1.22 KB
/
data_analysis_example.py
File metadata and controls
57 lines (42 loc) · 1.22 KB
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
import logging
import os
from os import getenv
import requests
from dotenv import load_dotenv
from e2b.templates.data_analysis import DataAnalysis
load_dotenv()
E2B_API_KEY = getenv("E2B_API_KEY")
logging.basicConfig(level=logging.ERROR)
def main():
s = DataAnalysis(api_key=E2B_API_KEY)
os.makedirs("data", exist_ok=True)
with open("data/netflix.csv", "wb") as f:
response = requests.get(
"https://storage.googleapis.com/e2b-examples/netflix.csv"
)
f.write(response.content)
with open("data/netflix.csv", "rb") as f:
path = s.upload_file(file=f)
stdout, stderr, artifacts = s.run_python(
f"""
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('{path}')
top_countries = data['country'].value_counts().head(10)
plt.figure(figsize=(10, 6))
top_countries.plot(kind='bar', color='skyblue')
plt.title('Number of content')
plt.xlabel('Country')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.show()
"""
)
for artifact in artifacts:
content = artifact.download()
with open(os.path.split(artifact.name)[-1], "wb") as f:
f.write(content)
s.close()
if __name__ == "__main__":
main()