实现输入命令获取返回结果的功能
import subprocess
import gradio as gr
with gr.Blocks() as demo:
def get_cmd_result(cmd):
res=subprocess.checkout(cmd,shell=True)
res=res.decode('utf-8')
def get_ip_result()
res=subprocess.checkout(cmd,shell=True)
return res.decode("utf-8").strip()
with gr.Tab("Xshell功能模拟") as linux_tab:
with gr.column() as col:
ip_result=gr.Textbox(label="当前运行机器")
linuc_cmd=gr.Textbox(value ="pwd",label="输入查询命令")
cmd_btn=gr.Button("执行命令")
cmd_result=gr.Textbox(label="命令执行结果")
cmd_btn.click(get_ip_result,outputs=ip_result)
cmd_btn.click(get_cmd_result,inputs=linux_cmd,outputs=cmd_result)
with gr.Tab("备用") as linux_tab:
ip_result=gr.Textbox(label="当前运行机器")
# 因为在虚拟机中启动,而不是pycharm 所以指定主机ip (1.1.1.1)和端口号,如果是在pycharm 中,无需指定
demo.launch(server_name=“1.1.1.1”,server_port=7860,share=True,inbrowser=True)