Skip to content

Commit b32c2c0

Browse files
committed
update examples
1 parent eec6072 commit b32c2c0

File tree

4 files changed

+22
-57
lines changed

4 files changed

+22
-57
lines changed

examples/big_upload.py renamed to examples/big_upload_from_url.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from codeboxapi import CodeBox
22

33

4-
def url_upload(codebox, url: str) -> None:
5-
codebox.run(
4+
def url_upload(codebox: CodeBox, url: str) -> None:
5+
codebox.exec(
66
"""
77
import requests
88
@@ -16,7 +16,7 @@ def download_file_from_url(url: str) -> None:
1616
file.write(chunk)
1717
"""
1818
)
19-
print(codebox.run(f"download_file_from_url('{url}')"))
19+
print(codebox.exec(f"download_file_from_url('{url}')"))
2020

2121

2222
codebox = CodeBox()
@@ -33,6 +33,6 @@ def download_file_from_url(url: str) -> None:
3333
)
3434
print(codebox.list_files())
3535

36-
codebox.run("import os")
37-
print(codebox.run("print(os.listdir())"))
38-
print(codebox.run("print([(f, os.path.getsize(f)) for f in os.listdir('.')])"))
36+
codebox.exec("import os")
37+
print(codebox.exec("print(os.listdir())"))
38+
print(codebox.exec("print([(f, os.path.getsize(f)) for f in os.listdir('.')])"))

examples/file_io.py renamed to examples/file_conversion.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
codebox.install("openpyxl")
1515

1616
# convert dataset csv to excel
17-
output = codebox.run(
17+
output = codebox.exec(
1818
"import pandas as pd\n\n"
1919
"df = pd.read_csv('iris.csv', header=None)\n\n"
2020
"df.to_excel('iris.xlsx', index=False)\n"
2121
"'iris.xlsx'"
2222
)
2323

2424
# check output type
25-
if output.type == "image/png":
25+
if output.images:
2626
print("This should not happen")
27-
elif output.type == "error":
28-
print("Error: ", output.content)
27+
elif output.errors:
28+
print("Error: ", output.errors)
2929
else:
3030
# all files inside the codebox
3131
for file in codebox.list_files():
3232
print("File: ", file.path)
33-
print("Content: ", file.get_content())
33+
print("File Size: ", file.get_size())

examples/parallel.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/plot_dataset.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,34 @@
1-
import os
1+
import base64
2+
from io import BytesIO
23
from pathlib import Path
34

45
import httpx
56
from codeboxapi import CodeBox
7+
from PIL import Image
68

79
codebox = CodeBox(api_key="local")
810

911
# download the iris dataset
10-
csv_bytes = httpx.get(
12+
iris_csv_bytes = httpx.get(
1113
"https://archive.ics.uci.edu/" "ml/machine-learning-databases/iris/iris.data"
1214
).content
1315

1416
# upload the dataset to the codebox
15-
codebox.upload("iris.csv", csv_bytes)
17+
codebox.upload("iris.csv", iris_csv_bytes)
1618

1719
# dataset analysis code
1820
file_path = Path("examples/assets/dataset_code.txt")
1921

2022
# run the code
21-
output = codebox.run(code=file_path)
22-
print(output)
23-
print(output.type)
24-
25-
if output.type == "image" and os.environ.get("CODEBOX_TEST") == "False":
26-
try:
27-
from PIL import Image # type: ignore
28-
except ImportError:
29-
print(
30-
"Please install it with "
31-
'`pip install "codeboxapi[image_support]"`'
32-
" to display images."
33-
)
34-
exit(1)
35-
36-
# Convert the image content ( bytes) into an image
37-
import base64
38-
from io import BytesIO
39-
40-
img_bytes = base64.b64decode(output.content)
23+
output = codebox.exec(file_path)
24+
25+
if output.images:
26+
img_bytes = base64.b64decode(output.images[0])
4127
img_buffer = BytesIO(img_bytes)
4228

4329
# Display the image
4430
img = Image.open(img_buffer)
4531
img.show()
4632

47-
elif output.type == "error":
48-
# error output
49-
print("Error:")
50-
print(output.content)
33+
elif output.errors:
34+
print("Error:", output.errors)

0 commit comments

Comments
 (0)