-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstable_web_launcher.py
More file actions
131 lines (112 loc) · 4.2 KB
/
Copy pathstable_web_launcher.py
File metadata and controls
131 lines (112 loc) · 4.2 KB
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
#!/usr/bin/env python3
"""
Stable Web Demo Launcher
Ensures the Streamlit server stays running reliably
"""
import subprocess
import sys
import time
import os
import signal
from pathlib import Path
class WebDemoLauncher:
def __init__(self):
self.process = None
self.running = False
def cleanup(self, signum=None, frame=None):
"""Clean shutdown"""
print("\n🛑 Shutting down web demo...")
self.running = False
if self.process:
try:
self.process.terminate()
self.process.wait(timeout=5)
except:
if self.process:
self.process.kill()
print("✅ Web demo stopped cleanly")
sys.exit(0)
def start_server(self, port=8507):
"""Start Streamlit server with error handling"""
cmd = [
sys.executable, "-m", "streamlit", "run", "web_demo.py",
"--server.port", str(port),
"--server.address", "0.0.0.0",
"--server.headless", "true",
"--browser.gatherUsageStats", "false",
"--server.enableCORS", "false",
"--server.enableXsrfProtection", "false"
]
try:
self.process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1
)
return True
except Exception as e:
print(f"❌ Failed to start server: {e}")
return False
def monitor_server(self, port=8507):
"""Monitor server and restart if needed"""
print("🚀 Starting Pandas-TA Web Demo")
print("=" * 40)
print(f"📱 Local URL: http://localhost:{port}")
print(f"📱 Network URL: http://10.0.0.214:{port}")
print("🔄 Server will auto-restart if it crashes")
print("⏹️ Press Ctrl+C to stop")
print("=" * 40)
# Set up signal handlers
signal.signal(signal.SIGINT, self.cleanup)
signal.signal(signal.SIGTERM, self.cleanup)
self.running = True
restart_count = 0
while self.running:
if not self.start_server(port):
time.sleep(5)
continue
print(f"✅ Web demo started (attempt #{restart_count + 1})")
# Monitor the process
while self.running and self.process:
try:
# Check if process is still running
return_code = self.process.poll()
if return_code is not None:
print(f"⚠️ Server exited with code {return_code}")
break
# Read output
line = self.process.stdout.readline()
if line:
line = line.strip()
if "You can now view your Streamlit app" in line:
print("🌐 Server is ready!")
elif "Stopping..." in line:
print("🔄 Server stopping...")
elif "ERROR" in line or "Exception" in line:
print(f"❌ Error: {line}")
time.sleep(0.1)
except Exception as e:
print(f"❌ Monitor error: {e}")
break
# If we get here, the server stopped
if self.running:
restart_count += 1
print(f"🔄 Restarting server (attempt #{restart_count + 1})...")
time.sleep(2)
self.cleanup()
def main():
"""Main function"""
# Change to the correct directory
script_dir = Path(__file__).parent
os.chdir(script_dir)
# Check if web_demo.py exists
if not Path("web_demo.py").exists():
print("❌ web_demo.py not found in current directory")
return
# Start the launcher
launcher = WebDemoLauncher()
launcher.monitor_server()
if __name__ == "__main__":
main()