File size: 825 Bytes
7a81bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import gradio as gr
import subprocess
import shlex

def run_command(cmd, history):
    try:
        args = shlex.split(cmd)
        result = subprocess.run(args, capture_output=True, text=True, timeout=10)
        output = result.stdout or result.stderr
    except Exception as e:
        output = str(e)
    new_history = history + f"> {cmd}\n{output}\n"
    return new_history, new_history

with gr.Blocks() as demo:
    gr.Markdown("### 🖥️ Simple Web Terminal")
    history = gr.Textbox(label="Terminal", lines=20, interactive=False)
    cmd_input = gr.Textbox(label="Command")
    run_btn = gr.Button("Run")
    
    run_btn.click(run_command, inputs=[cmd_input, history], outputs=[history, history])
    cmd_input.submit(run_command, inputs=[cmd_input, history], outputs=[history, history])

demo.launch()