Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,38 +10,30 @@ MAX_LOG_ENTRIES = 1000
|
|
10 |
|
11 |
def run_cli_script():
|
12 |
"""Runs cli.py and streams logs in real-time to both UI and terminal."""
|
|
|
|
|
13 |
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
14 |
-
log_entry = {'time': timestamp, 'output': '', 'error': ''}
|
15 |
|
16 |
try:
|
17 |
process = subprocess.Popen(
|
18 |
["python", "cli.py"],
|
19 |
stdout=subprocess.PIPE,
|
20 |
-
stderr=subprocess.STDOUT,
|
21 |
text=True,
|
22 |
bufsize=1
|
23 |
)
|
24 |
|
25 |
-
# Stream logs to UI and print to terminal
|
26 |
for line in process.stdout:
|
27 |
-
|
28 |
-
|
29 |
-
print(line, end="") # ✅ Print logs to terminal
|
30 |
-
if len(execution_logs) > MAX_LOG_ENTRIES:
|
31 |
-
execution_logs.pop(0)
|
32 |
|
33 |
-
|
34 |
-
log_entry['error'] += line
|
35 |
-
execution_logs.append({'time': timestamp, 'output': '', 'error': line})
|
36 |
-
print(line, end="") # ✅ Print errors to terminal
|
37 |
if len(execution_logs) > MAX_LOG_ENTRIES:
|
38 |
-
execution_logs = execution_logs[-MAX_LOG_ENTRIES:]
|
39 |
-
|
40 |
|
41 |
except Exception as e:
|
42 |
-
log_entry['error'] = str(e)
|
43 |
execution_logs.append({'time': timestamp, 'output': '', 'error': str(e)})
|
44 |
-
print(f"Error: {str(e)}")
|
45 |
|
46 |
def start_initial_run():
|
47 |
threading.Thread(target=run_cli_script, daemon=True).start()
|
|
|
10 |
|
11 |
def run_cli_script():
|
12 |
"""Runs cli.py and streams logs in real-time to both UI and terminal."""
|
13 |
+
global execution_logs # Declare the global variable
|
14 |
+
|
15 |
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
|
|
16 |
|
17 |
try:
|
18 |
process = subprocess.Popen(
|
19 |
["python", "cli.py"],
|
20 |
stdout=subprocess.PIPE,
|
21 |
+
stderr=subprocess.STDOUT, # Merge stdout and stderr
|
22 |
text=True,
|
23 |
bufsize=1
|
24 |
)
|
25 |
|
|
|
26 |
for line in process.stdout:
|
27 |
+
execution_logs.append({'time': timestamp, 'output': line.strip(), 'error': ''})
|
28 |
+
print(line, end="")
|
|
|
|
|
|
|
29 |
|
30 |
+
# Trim logs efficiently
|
|
|
|
|
|
|
31 |
if len(execution_logs) > MAX_LOG_ENTRIES:
|
32 |
+
execution_logs = execution_logs[-MAX_LOG_ENTRIES:]
|
|
|
33 |
|
34 |
except Exception as e:
|
|
|
35 |
execution_logs.append({'time': timestamp, 'output': '', 'error': str(e)})
|
36 |
+
print(f"Error: {str(e)}")
|
37 |
|
38 |
def start_initial_run():
|
39 |
threading.Thread(target=run_cli_script, daemon=True).start()
|