File size: 1,980 Bytes
4e80164
 
 
5e18983
347834d
9fb4186
347834d
5e18983
9fb4186
20c9a0d
5e18983
9fb4186
347834d
20c9a0d
5e18983
9fb4186
20c9a0d
347834d
 
 
20c9a0d
5e18983
 
9fb4186
5e18983
 
20c9a0d
 
 
9fb4186
 
20c9a0d
 
 
4e80164
20c9a0d
4e80164
20c9a0d
 
 
 
347834d
20c9a0d
 
347834d
20c9a0d
347834d
20c9a0d
 
4e80164
20c9a0d
 
9fb4186
4e80164
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import io
import traceback
import matplotlib.pyplot as plt
from IPython.display import display
from IPython.core.interactiveshell import InteractiveShell
from IPython.utils.capture import capture_output

def run_jupyter_style_code(code):
    plt.close('all')  # Close any old plots

    shell = InteractiveShell.instance()
    img_path = None
    output = ""

    try:
        # Capture output (stdout, stderr) and display anything that gets returned
        with capture_output(display=True) as captured:
            exec(code, {"plt": plt, "pd": __import__('pandas'), "display": display})

        # Get any figures generated by Matplotlib
        figures = [plt.figure(i) for i in plt.get_fignums()]
        if figures:
            img_path = "plot.png"
            figures[-1].savefig(img_path)

        # Join captured output (stdout + rich outputs like HTML or plots)
        output = captured.stdout + ''.join(str(out) for out in captured.outputs)
        return output, img_path

    except Exception as e:
        # If an error occurs, return the traceback as output
        output = traceback.format_exc()
        return output, None

# Create the Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🧪 Python Code Runner with Jupyter-style Terminal")

    # Code input area (multi-line text box simulating terminal)
    code_input = gr.Textbox(label="Python Terminal", placeholder="Enter Python code...", lines=10)
    
    # Output for the console/logs (including rich content)
    output_text = gr.Textbox(label="Console Output", lines=12, interactive=False)
    
    # Display for any generated plots
    plot_output = gr.Image(label="Generated Plot", type="filepath")
    
    # Button to execute the code
    run_button = gr.Button("Run Code")
    
    # Trigger execution on button click
    run_button.click(fn=run_jupyter_style_code, inputs=code_input, outputs=[output_text, plot_output])

if __name__ == "__main__":
    demo.launch()