Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,46 +7,49 @@ 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 (
|
19 |
with capture_output(display=True) as captured:
|
20 |
exec(code, {"plt": plt, "pd": __import__('pandas'), "display": display})
|
21 |
|
22 |
-
#
|
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 |
-
#
|
29 |
-
|
30 |
-
return
|
31 |
|
32 |
except Exception as e:
|
33 |
-
#
|
34 |
-
|
|
|
35 |
|
36 |
-
|
37 |
-
# Gradio Interface
|
38 |
with gr.Blocks() as demo:
|
39 |
-
gr.Markdown("## 🧪 Python Code Runner with Jupyter-style
|
|
|
|
|
|
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
output_text = gr.HTML(label="Console Output + Rich Result")
|
46 |
plot_output = gr.Image(label="Generated Plot", type="filepath")
|
47 |
-
|
48 |
-
#
|
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 |
|
52 |
if __name__ == "__main__":
|
|
|
7 |
from IPython.utils.capture import capture_output
|
8 |
|
9 |
def run_jupyter_style_code(code):
|
10 |
+
plt.close('all') # Close any old plots
|
|
|
11 |
|
|
|
12 |
shell = InteractiveShell.instance()
|
13 |
img_path = None
|
14 |
+
output = ""
|
15 |
|
16 |
try:
|
17 |
+
# Capture output (stdout, stderr) and display anything that gets returned
|
18 |
with capture_output(display=True) as captured:
|
19 |
exec(code, {"plt": plt, "pd": __import__('pandas'), "display": display})
|
20 |
|
21 |
+
# Get any figures generated by Matplotlib
|
22 |
figures = [plt.figure(i) for i in plt.get_fignums()]
|
23 |
if figures:
|
24 |
img_path = "plot.png"
|
25 |
figures[-1].savefig(img_path)
|
26 |
|
27 |
+
# Join captured output (stdout + rich outputs like HTML or plots)
|
28 |
+
output = captured.stdout + ''.join(str(out) for out in captured.outputs)
|
29 |
+
return output, img_path
|
30 |
|
31 |
except Exception as e:
|
32 |
+
# If an error occurs, return the traceback as output
|
33 |
+
output = traceback.format_exc()
|
34 |
+
return output, None
|
35 |
|
36 |
+
# Create the Gradio UI
|
|
|
37 |
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown("## 🧪 Python Code Runner with Jupyter-style Terminal")
|
39 |
+
|
40 |
+
# Code input area (multi-line text box simulating terminal)
|
41 |
+
code_input = gr.Textbox(label="Python Terminal", placeholder="Enter Python code...", lines=10)
|
42 |
|
43 |
+
# Output for the console/logs (including rich content)
|
44 |
+
output_text = gr.Textbox(label="Console Output", lines=12, interactive=False)
|
45 |
|
46 |
+
# Display for any generated plots
|
|
|
47 |
plot_output = gr.Image(label="Generated Plot", type="filepath")
|
48 |
+
|
49 |
+
# Button to execute the code
|
50 |
run_button = gr.Button("Run Code")
|
51 |
+
|
52 |
+
# Trigger execution on button click
|
53 |
run_button.click(fn=run_jupyter_style_code, inputs=code_input, outputs=[output_text, plot_output])
|
54 |
|
55 |
if __name__ == "__main__":
|