1+ import os
2+
13import requests
24
35from 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
8170if __name__ == "__main__" :
8271 import asyncio
0 commit comments