Update app.py
Browse files
app.py
CHANGED
@@ -7,13 +7,7 @@ import requests
|
|
7 |
import json
|
8 |
import re
|
9 |
|
10 |
-
|
11 |
-
def get_image_data(fig):
|
12 |
-
img_byte_arr = io.BytesIO()
|
13 |
-
img.save(img_byte_arr, format='PNG')
|
14 |
-
img_byte_arr = img_byte_arr.getvalue()
|
15 |
-
img_str = base64.b64encode(img_byte_arr).decode('utf-8')
|
16 |
-
return img_str
|
17 |
|
18 |
# 执行 Python 代码并生成图像
|
19 |
def execute_code(code):
|
@@ -21,7 +15,14 @@ def execute_code(code):
|
|
21 |
exec(code, namespace)
|
22 |
fig = namespace.get('fig') # Assume the code generates a matplotlib figure named 'fig'
|
23 |
if fig:
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
else:
|
26 |
raise ValueError("The code did not generate a matplotlib figure named 'fig'")
|
27 |
|
|
|
7 |
import json
|
8 |
import re
|
9 |
|
10 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# 执行 Python 代码并生成图像
|
13 |
def execute_code(code):
|
|
|
15 |
exec(code, namespace)
|
16 |
fig = namespace.get('fig') # Assume the code generates a matplotlib figure named 'fig'
|
17 |
if fig:
|
18 |
+
img = get_image_data(fig)
|
19 |
+
|
20 |
+
img_byte_arr = io.BytesIO()
|
21 |
+
img.save(img_byte_arr, format='PNG')
|
22 |
+
img_byte_arr = img_byte_arr.getvalue()
|
23 |
+
img_b64 = base64.b64encode(img_byte_arr).decode('utf-8')
|
24 |
+
|
25 |
+
return img_b64
|
26 |
else:
|
27 |
raise ValueError("The code did not generate a matplotlib figure named 'fig'")
|
28 |
|