Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -53,10 +53,22 @@ def execute_code(code: str):
|
|
53 |
if err.getvalue():
|
54 |
output_text += "\nErrors:\n" + err.getvalue()
|
55 |
|
56 |
-
# Check if there's a plot
|
57 |
if plt.gcf().axes:
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
plt.close()
|
|
|
|
|
|
|
60 |
|
61 |
# Check for DataFrame output
|
62 |
for var in namespace:
|
@@ -108,7 +120,7 @@ print(df.head())
|
|
108 |
|
109 |
with gr.Column():
|
110 |
output_text = gr.Textbox(label="Output", lines=5)
|
111 |
-
output_plot = gr.
|
112 |
output_df = gr.Dataframe(label="DataFrame Output")
|
113 |
|
114 |
# Handle code execution
|
|
|
53 |
if err.getvalue():
|
54 |
output_text += "\nErrors:\n" + err.getvalue()
|
55 |
|
56 |
+
# Check if there's a plot and convert to image
|
57 |
if plt.gcf().axes:
|
58 |
+
# Save plot to a temporary file
|
59 |
+
import tempfile
|
60 |
+
import os
|
61 |
+
|
62 |
+
# Create a temporary file with .png extension
|
63 |
+
temp_dir = tempfile.gettempdir()
|
64 |
+
temp_file = os.path.join(temp_dir, f"plot_{os.getpid()}.png")
|
65 |
+
|
66 |
+
# Save the current figure
|
67 |
+
plt.savefig(temp_file, bbox_inches='tight', dpi=300)
|
68 |
plt.close()
|
69 |
+
|
70 |
+
# Set the output_plot to the file path
|
71 |
+
output_plot = temp_file
|
72 |
|
73 |
# Check for DataFrame output
|
74 |
for var in namespace:
|
|
|
120 |
|
121 |
with gr.Column():
|
122 |
output_text = gr.Textbox(label="Output", lines=5)
|
123 |
+
output_plot = gr.Image(label="Plot Output")
|
124 |
output_df = gr.Dataframe(label="DataFrame Output")
|
125 |
|
126 |
# Handle code execution
|