terminal / app.py
rastof9's picture
Create app.py
7a81bf6 verified
raw
history blame contribute delete
825 Bytes
# 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()