File tree Expand file tree Collapse file tree 4 files changed +22
-57
lines changed Expand file tree Collapse file tree 4 files changed +22
-57
lines changed Original file line number Diff line number Diff line change 11from 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 """
77import 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
2222codebox = CodeBox ()
@@ -33,6 +33,6 @@ def download_file_from_url(url: str) -> None:
3333)
3434print (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('.')])" ))
Original file line number Diff line number Diff line change 1414codebox .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 )
2929else :
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 ())
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- import os
1+ import base64
2+ from io import BytesIO
23from pathlib import Path
34
45import httpx
56from codeboxapi import CodeBox
7+ from PIL import Image
68
79codebox = 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
1820file_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 )
You can’t perform that action at this time.
0 commit comments