File size: 8,791 Bytes
35dc940 | 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | import gradio as gr
import subprocess
import os
import sys
class Terminal:
def __init__(self):
self.current_dir = "/app"
self.history = []
self.output_history = []
self.max_history = 100
def execute(self, command):
"""执行命令并返回结果"""
if not command.strip():
return {"output": "", "success": True, "dir": self.current_dir}
cmd = command.strip()
self.history.append(cmd)
if len(self.history) > self.max_history:
self.history.pop(0)
# 处理 cd 命令
if cmd.startswith("cd "):
try:
target = cmd[3:].strip()
if not target:
target = os.path.expanduser("~")
if not os.path.isabs(target):
target = os.path.join(self.current_dir, target)
target = os.path.normpath(target)
if os.path.isdir(target):
os.chdir(target)
self.current_dir = os.getcwd()
output = f"Changed directory to: {self.current_dir}"
self.output_history.append(f"$ {cmd}\n{output}")
return {"output": output, "success": True, "dir": self.current_dir}
else:
output = f"cd: {target}: No such directory"
self.output_history.append(f"$ {cmd}\n{output}")
return {"output": output, "success": False, "dir": self.current_dir}
except Exception as e:
output = f"cd error: {str(e)}"
self.output_history.append(f"$ {cmd}\n{output}")
return {"output": output, "success": False, "dir": self.current_dir}
# 处理 clear 命令
if cmd == "clear" or cmd == "cls":
self.output_history = []
return {"output": "", "success": True, "dir": self.current_dir, "clear": True}
# 执行其他命令
try:
original_dir = os.getcwd()
os.chdir(self.current_dir)
process = subprocess.run(
["bash", "-c", cmd],
capture_output=True,
text=True,
timeout=30,
env=os.environ
)
os.chdir(original_dir)
# 构建输出
output_lines = []
if process.stdout:
output_lines.append(process.stdout.rstrip())
if process.stderr:
output_lines.append(process.stderr.rstrip())
if process.returncode != 0 and not process.stderr:
output_lines.append(f"[Exit code: {process.returncode}]")
output = "\n".join(output_lines)
# 保存到历史
self.output_history.append(f"$ {cmd}\n{output}" if output else f"$ {cmd}")
return {
"output": output,
"success": process.returncode == 0,
"dir": self.current_dir
}
except subprocess.TimeoutExpired:
output = "Error: Command timed out after 30 seconds"
self.output_history.append(f"$ {cmd}\n{output}")
return {"output": output, "success": False, "dir": self.current_dir}
except Exception as e:
output = f"Error: {str(e)}"
self.output_history.append(f"$ {cmd}\n{output}")
return {"output": output, "success": False, "dir": self.current_dir}
def get_full_output(self):
"""获取完整的终端输出"""
if not self.output_history:
return f"{self.current_dir}$ "
# 只保留最近20条命令以避免过长
recent_history = self.output_history[-20:]
full_output = "\n\n".join(recent_history)
full_output += f"\n\n{self.current_dir}$ "
return full_output
# 创建终端实例
terminal = Terminal()
# 自定义CSS(现在放在launch()方法中)
custom_css = """
#terminal-output textarea {
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
font-size: 14px !important;
background-color: #1e1e1e !important;
color: #e0e0e0 !important;
border: 1px solid #444 !important;
}
#terminal-output {
max-height: 500px;
overflow-y: auto;
}
.prompt-text {
color: #4CAF50;
font-weight: bold;
}
.command-text {
color: #64B5F6;
}
.error-text {
color: #f44336;
}
#cmd-input input {
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 14px;
}
.quick-btn {
font-family: monospace;
font-size: 12px;
margin: 2px;
padding: 4px 8px;
}
"""
# 创建界面(移除了css参数)
with gr.Blocks(title="Ubuntu Terminal") as demo:
gr.Markdown("# 🐧 Ubuntu 22.04 Terminal")
gr.Markdown(f"**Current directory:** `{terminal.current_dir}`")
# 使用 Textbox 显示终端输出
terminal_output = gr.Textbox(
label="",
value=terminal.get_full_output(),
lines=25,
elem_id="terminal-output",
interactive=False,
show_label=False
)
# 命令输入
with gr.Row():
cmd_input = gr.Textbox(
label="",
placeholder="Enter command (press Enter to execute)...",
scale=8,
elem_id="cmd-input",
show_label=False
)
execute_btn = gr.Button("Run", variant="primary", scale=1)
clear_btn = gr.Button("Clear", variant="secondary", scale=1)
# 快速命令按钮
gr.Markdown("### 🚀 Quick Commands")
with gr.Row():
quick_commands = [
("pwd", "Show path"),
("ls -la", "List files"),
("python3 --version", "Python"),
("wget --version", "Wget"),
("curl --version", "Curl"),
("git --version", "Git"),
("sudo whoami", "Sudo test"),
("uname -a", "System info"),
]
for cmd, desc in quick_commands:
btn = gr.Button(cmd, size="sm", min_width=100)
btn.click(
fn=lambda c=cmd: c,
outputs=[cmd_input]
)
# 信息面板
with gr.Accordion("📋 System Information", open=False):
# 安全地获取用户信息
try:
user = os.environ.get('USER') or os.environ.get('USERNAME') or 'ubuntu'
uid = os.getuid()
except:
user = 'ubuntu'
uid = 1000
gr.Markdown(f"""
**Available Tools:**
- ✅ Python 3, pip
- ✅ sudo (passwordless for 'ubuntu' user)
- ✅ wget, curl, git
- ✅ vim, nano, htop
- ✅ build-essential
**Current User:** `{user}` (uid: {uid})
**Home Directory:** `{os.path.expanduser('~')}`
**Try these commands:**
```bash
# File operations
echo "Hello" > test.txt
cat test.txt
rm test.txt
# System info
df -h
free -h
ps aux | head -20
# Network test
curl -s https://httpbin.org/ip
ping -c 1 8.8.8.8 2>/dev/null || echo "ping not available"
```
""")
# 事件处理函数
def execute_command(cmd, current_output):
"""执行命令并更新输出"""
result = terminal.execute(cmd)
new_output = terminal.get_full_output()
return new_output, ""
def clear_terminal():
"""清空终端"""
terminal.output_history = []
return terminal.get_full_output()
# 绑定事件
execute_btn.click(
execute_command,
inputs=[cmd_input, terminal_output],
outputs=[terminal_output, cmd_input]
)
cmd_input.submit(
execute_command,
inputs=[cmd_input, terminal_output],
outputs=[terminal_output, cmd_input]
)
clear_btn.click(
clear_terminal,
outputs=[terminal_output]
)
if __name__ == "__main__":
print("=" * 60)
print("🚀 Starting Terminal Application")
print("📁 Current directory:", terminal.current_dir)
# 安全地获取用户信息
try:
user = os.environ.get('USER') or os.environ.get('USERNAME') or 'ubuntu'
except:
user = 'ubuntu'
print("👤 User:", user)
print("=" * 60)
# 启动应用(css参数现在在这里)
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
css=custom_css # CSS参数移到这里
) |