Devity4756 commited on
Commit
7fd8b51
·
verified ·
1 Parent(s): 7cdbef0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -4
app.py CHANGED
@@ -240,6 +240,65 @@ def run_flask_app(app_name: str, code: str):
240
  except Exception as e:
241
  log(f"Error starting Flask app {app_name}: {e}")
242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  def view_logs(app_name: str):
244
  """Get logs of a Flask app"""
245
  if app_name not in flask_logs:
@@ -270,7 +329,7 @@ def get_flask_apps():
270
  return result
271
 
272
  def run_command(cmd: str):
273
- """Handle terminal-like commands including Flask"""
274
  global current_dir, running_process, open_file_path
275
 
276
  try:
@@ -284,7 +343,7 @@ def run_command(cmd: str):
284
  return f"$ {cmd}\n\nStopped running process.", "", None
285
  return f"$ {cmd}\n\nNo active process to stop.", "", None
286
 
287
- # === Start Flask app ===
288
  if raw_cmd.startswith("flaskrun "):
289
  parts = raw_cmd.split(" ", 2)
290
  if len(parts) < 3:
@@ -293,6 +352,24 @@ def run_command(cmd: str):
293
  threading.Thread(target=run_flask_app, args=(appname, code), daemon=True).start()
294
  return f"$ {cmd}\n\nStarting Flask app '{appname}'... Check status with 'flasklist' or view logs with 'logs {appname}'", "", None
295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  # === List Flask apps ===
297
  if raw_cmd == "flasklist":
298
  apps_info = get_flask_apps()
@@ -417,14 +494,15 @@ def save_file(new_content: str, file_path: str):
417
  # === Gradio UI ===
418
  with gr.Blocks() as demo:
419
  gr.Markdown("## 🖥️ Secure Terminal + Editor + Flask Apps")
420
- gr.Markdown("Create Flask apps with `flaskrun <name> <code>` - they will be accessible at the URLs shown in the logs")
421
 
422
  with gr.Row():
423
  cmd = gr.Textbox(
424
  label="Command",
425
  placeholder="Examples:\n"
426
  "flaskrun myapp '@app.route(\"/hello\")\\ndef hello(): return \"Hello World!\"'\n"
427
- "flaskrun api '@app.route(\"/data\")\\ndef data(): return {\"message\": \"API working!\"}'\n"
 
428
  "flasklist\n"
429
  "logs myapp\n"
430
  "cd Alex/project\n"
 
240
  except Exception as e:
241
  log(f"Error starting Flask app {app_name}: {e}")
242
 
243
+ def run_python_app(file_path: str):
244
+ """Run a regular Python application"""
245
+ target = expand_path(file_path)
246
+
247
+ if not os.path.exists(target):
248
+ return f"File not found: {file_path}"
249
+
250
+ if not target.endswith('.py'):
251
+ return f"Not a Python file: {file_path}"
252
+
253
+ def run_app():
254
+ try:
255
+ process = subprocess.Popen(
256
+ ['python3', target],
257
+ cwd=os.path.dirname(target),
258
+ stdout=subprocess.PIPE,
259
+ stderr=subprocess.PIPE,
260
+ text=True
261
+ )
262
+ stdout, stderr = process.communicate()
263
+
264
+ if stdout:
265
+ print(f"Output from {file_path}:\n{stdout}")
266
+ if stderr:
267
+ print(f"Errors from {file_path}:\n{stderr}")
268
+
269
+ except Exception as e:
270
+ print(f"Error running {file_path}: {e}")
271
+
272
+ # Run in a separate thread
273
+ thread = threading.Thread(target=run_app, daemon=True)
274
+ thread.start()
275
+
276
+ return f"Started Python application: {file_path}"
277
+
278
+ def is_flask_app(file_path: str):
279
+ """Check if a Python file contains Flask app code"""
280
+ target = expand_path(file_path)
281
+
282
+ if not os.path.exists(target) or not target.endswith('.py'):
283
+ return False
284
+
285
+ try:
286
+ with open(target, 'r', encoding='utf-8') as f:
287
+ content = f.read()
288
+
289
+ # Check for Flask patterns
290
+ flask_patterns = [
291
+ 'from flask import',
292
+ 'import flask',
293
+ 'Flask(__name__)',
294
+ 'app = Flask',
295
+ '@app.route'
296
+ ]
297
+
298
+ return any(pattern in content for pattern in flask_patterns)
299
+ except:
300
+ return False
301
+
302
  def view_logs(app_name: str):
303
  """Get logs of a Flask app"""
304
  if app_name not in flask_logs:
 
329
  return result
330
 
331
  def run_command(cmd: str):
332
+ """Handle terminal-like commands including Flask and Python apps"""
333
  global current_dir, running_process, open_file_path
334
 
335
  try:
 
343
  return f"$ {cmd}\n\nStopped running process.", "", None
344
  return f"$ {cmd}\n\nNo active process to stop.", "", None
345
 
346
+ # === Start Flask app from code ===
347
  if raw_cmd.startswith("flaskrun "):
348
  parts = raw_cmd.split(" ", 2)
349
  if len(parts) < 3:
 
352
  threading.Thread(target=run_flask_app, args=(appname, code), daemon=True).start()
353
  return f"$ {cmd}\n\nStarting Flask app '{appname}'... Check status with 'flasklist' or view logs with 'logs {appname}'", "", None
354
 
355
+ # === Run Python application ===
356
+ if raw_cmd.startswith("python3 "):
357
+ file_path = raw_cmd[8:].strip()
358
+ if is_flask_app(file_path):
359
+ # Read the Flask app code and run it
360
+ try:
361
+ with open(expand_path(file_path), 'r', encoding='utf-8') as f:
362
+ code = f.read()
363
+ app_name = os.path.basename(file_path).replace('.py', '')
364
+ threading.Thread(target=run_flask_app, args=(app_name, code), daemon=True).start()
365
+ return f"$ {cmd}\n\nDetected Flask app. Starting '{app_name}'... Check status with 'flasklist'", "", None
366
+ except Exception as e:
367
+ return f"$ {cmd}\n\nError reading Flask app: {e}", "", None
368
+ else:
369
+ # Run as regular Python script
370
+ result = run_python_app(file_path)
371
+ return f"$ {cmd}\n\n{result}", "", None
372
+
373
  # === List Flask apps ===
374
  if raw_cmd == "flasklist":
375
  apps_info = get_flask_apps()
 
494
  # === Gradio UI ===
495
  with gr.Blocks() as demo:
496
  gr.Markdown("## 🖥️ Secure Terminal + Editor + Flask Apps")
497
+ gr.Markdown("Run Flask apps or regular Python applications")
498
 
499
  with gr.Row():
500
  cmd = gr.Textbox(
501
  label="Command",
502
  placeholder="Examples:\n"
503
  "flaskrun myapp '@app.route(\"/hello\")\\ndef hello(): return \"Hello World!\"'\n"
504
+ "python3 app.py # Auto-detects Flask apps\n"
505
+ "python3 script.py # Runs regular Python scripts\n"
506
  "flasklist\n"
507
  "logs myapp\n"
508
  "cd Alex/project\n"