Skip to content

Commit 1c505f8

Browse files
committed
🧰 cleanup examples
1 parent 0c2bc00 commit 1c505f8

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

examples/async_plot_dataset.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import requests
24

35
from codeboxapi import CodeBox
@@ -11,16 +13,13 @@ async def main():
1113
"ml/machine-learning-databases/iris/iris.data"
1214
).content
1315

14-
print("downloaded dataset")
15-
1616
# upload the dataset to the codebox
1717
await codebox.aupload("iris.csv", csv_bytes)
1818

19-
print("Installing matplotlib and pandas")
19+
# install the required packages
2020
await codebox.ainstall("matplotlib")
2121
await codebox.ainstall("pandas")
2222

23-
print("Installed")
2423
# dataset analysis code
2524
code = (
2625
"import pandas as pd\n"
@@ -40,11 +39,7 @@ async def main():
4039
output = await codebox.arun(code)
4140
print(output.type)
4241

43-
if output.type == "image/png":
44-
# Convert the image content into an image
45-
import base64
46-
from io import BytesIO
47-
42+
if output.type == "image/png" and os.environ.get("CODEBOX_TEST") == "False":
4843
try:
4944
from PIL import Image # type: ignore
5045
except ImportError:
@@ -55,28 +50,22 @@ async def main():
5550
)
5651
exit(1)
5752

58-
# Decode the base64 string into bytes
59-
img_bytes = base64.b64decode(output.content)
60-
61-
# Create a BytesIO object
62-
img_io = BytesIO(img_bytes)
53+
# Convert the image content ( bytes) into an image
54+
import base64
55+
from io import BytesIO
6356

64-
# Use PIL to open the image
65-
img = Image.open(img_io)
57+
img_bytes = base64.b64decode(output.content)
58+
img_buffer = BytesIO(img_bytes)
6659

6760
# Display the image
61+
img = Image.open(img_buffer)
6862
img.show()
6963

7064
elif output.type == "error":
7165
# error output
7266
print("Error:")
7367
print(output.content)
7468

75-
else:
76-
# normal text output
77-
print("Text Output:")
78-
print(output.content)
79-
8069

8170
if __name__ == "__main__":
8271
import asyncio

examples/plot_dataset.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path
23

34
import requests
@@ -10,8 +11,6 @@
1011
"https://archive.ics.uci.edu/" "ml/machine-learning-databases/iris/iris.data"
1112
).content
1213

13-
print("downloaded dataset")
14-
1514
# upload the dataset to the codebox
1615
o = codebox.upload("iris.csv", csv_bytes)
1716

@@ -22,11 +21,7 @@
2221
output = codebox.run(file_path=file_path)
2322
print(output.type)
2423

25-
if output.type == "image/png":
26-
# Convert the image content into an image
27-
import base64
28-
from io import BytesIO
29-
24+
if output.type == "image/png" and os.environ.get("CODEBOX_TEST") == "False":
3025
try:
3126
from PIL import Image # type: ignore
3227
except ImportError:
@@ -37,24 +32,18 @@
3732
)
3833
exit(1)
3934

40-
# Decode the base64 string into bytes
41-
img_bytes = base64.b64decode(output.content)
42-
43-
# Create a BytesIO object
44-
img_io = BytesIO(img_bytes)
35+
# Convert the image content ( bytes) into an image
36+
import base64
37+
from io import BytesIO
4538

46-
# Use PIL to open the image
47-
img = Image.open(img_io)
39+
img_bytes = base64.b64decode(output.content)
40+
img_buffer = BytesIO(img_bytes)
4841

4942
# Display the image
43+
img = Image.open(img_buffer)
5044
img.show()
5145

5246
elif output.type == "error":
5347
# error output
5448
print("Error:")
5549
print(output.content)
56-
57-
else:
58-
# normal text output
59-
print("Text Output:")
60-
print(output.content)

0 commit comments

Comments
 (0)