Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,52 +2,50 @@ import gradio as gr
|
|
2 |
import io
|
3 |
import traceback
|
4 |
import matplotlib.pyplot as plt
|
5 |
-
|
6 |
-
from IPython.display import display, HTML
|
7 |
-
from contextlib import redirect_stdout
|
8 |
from IPython.core.interactiveshell import InteractiveShell
|
|
|
9 |
|
10 |
-
# Create a function to capture all kinds of Jupyter-like outputs
|
11 |
def run_jupyter_style_code(code):
|
12 |
-
#
|
13 |
plt.close('all')
|
14 |
|
15 |
-
#
|
16 |
shell = InteractiveShell.instance()
|
17 |
-
|
18 |
|
19 |
try:
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
if result.result is not None:
|
26 |
-
try:
|
27 |
-
display_out = display(result.result, display_id=False)
|
28 |
-
rich_output = str(result.result)
|
29 |
-
except Exception:
|
30 |
-
rich_output = str(result.result)
|
31 |
-
|
32 |
-
# Show latest matplotlib figure if available
|
33 |
figures = [plt.figure(i) for i in plt.get_fignums()]
|
34 |
-
img_path = None
|
35 |
if figures:
|
36 |
img_path = "plot.png"
|
37 |
figures[-1].savefig(img_path)
|
38 |
|
39 |
-
|
|
|
|
|
40 |
|
41 |
except Exception as e:
|
|
|
42 |
return traceback.format_exc(), None
|
43 |
|
|
|
|
|
44 |
with gr.Blocks() as demo:
|
45 |
gr.Markdown("## 🧪 Python Code Runner with Jupyter-style Output")
|
46 |
-
|
|
|
47 |
code_input = gr.Code(language="python", label="Enter Python Code")
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
|
|
51 |
run_button = gr.Button("Run Code")
|
52 |
run_button.click(fn=run_jupyter_style_code, inputs=code_input, outputs=[output_text, plot_output])
|
53 |
|
|
|
2 |
import io
|
3 |
import traceback
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
from IPython.display import display
|
|
|
|
|
6 |
from IPython.core.interactiveshell import InteractiveShell
|
7 |
+
from IPython.utils.capture import capture_output
|
8 |
|
|
|
9 |
def run_jupyter_style_code(code):
|
10 |
+
# Close any old plots
|
11 |
plt.close('all')
|
12 |
|
13 |
+
# Set up an interactive shell instance
|
14 |
shell = InteractiveShell.instance()
|
15 |
+
img_path = None
|
16 |
|
17 |
try:
|
18 |
+
# Capture output (both text and rich outputs like HTML, plots)
|
19 |
+
with capture_output(display=True) as captured:
|
20 |
+
exec(code, {"plt": plt, "pd": __import__('pandas'), "display": display})
|
21 |
+
|
22 |
+
# Check for any figures and save the last one as an image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
figures = [plt.figure(i) for i in plt.get_fignums()]
|
|
|
24 |
if figures:
|
25 |
img_path = "plot.png"
|
26 |
figures[-1].savefig(img_path)
|
27 |
|
28 |
+
# Return the captured HTML/text outputs
|
29 |
+
html_output = captured.stdout + ''.join(str(out) for out in captured.outputs)
|
30 |
+
return html_output, img_path
|
31 |
|
32 |
except Exception as e:
|
33 |
+
# Return any errors as string
|
34 |
return traceback.format_exc(), None
|
35 |
|
36 |
+
|
37 |
+
# Gradio Interface
|
38 |
with gr.Blocks() as demo:
|
39 |
gr.Markdown("## 🧪 Python Code Runner with Jupyter-style Output")
|
40 |
+
|
41 |
+
# Code input area
|
42 |
code_input = gr.Code(language="python", label="Enter Python Code")
|
43 |
+
|
44 |
+
# Outputs (HTML + Plot)
|
45 |
+
output_text = gr.HTML(label="Console Output + Rich Result")
|
46 |
+
plot_output = gr.Image(label="Generated Plot", type="filepath")
|
47 |
|
48 |
+
# Run button
|
49 |
run_button = gr.Button("Run Code")
|
50 |
run_button.click(fn=run_jupyter_style_code, inputs=code_input, outputs=[output_text, plot_output])
|
51 |
|