ParallelLLC/algorithmic_trading
2732
1#!/usr/bin/env python32"""3UI Launcher for Algorithmic Trading System4 5Provides multiple UI options:6- Streamlit: Quick prototyping and data science workflows7- Dash: Enterprise-grade interactive dashboards8- Jupyter: Notebook-based interfaces9- WebSocket: Real-time trading interfaces10"""11 12import argparse13import sys14import os15import subprocess16import webbrowser17import time18import threading19from typing import Optional20 21def check_dependencies():22 """Check if required UI dependencies are installed"""23 required_packages = [24 'streamlit',25 'dash',26 'plotly',27 'ipywidgets'28 ]29 30 missing_packages = []31 for package in required_packages:32 try:33 __import__(package)34 except ImportError:35 missing_packages.append(package)36 37 if missing_packages:38 print(f"โ Missing required packages: {', '.join(missing_packages)}")39 print("Please install them using: pip install -r requirements.txt")40 return False41 42 return True43 44def launch_streamlit():45 """Launch Streamlit application"""46 print("๐ Launching Streamlit UI...")47 48 # Create streamlit app file if it doesn't exist49 streamlit_app_path = "ui/streamlit_app.py"50 if not os.path.exists(streamlit_app_path):51 print(f"โ Streamlit app not found at {streamlit_app_path}")52 return False53 54 try:55 # Launch Streamlit56 cmd = [57 sys.executable, "-m", "streamlit", "run", 58 streamlit_app_path,59 "--server.port", "8501",60 "--server.address", "0.0.0.0",61 "--browser.gatherUsageStats", "false"62 ]63 64 print(f"Running: {' '.join(cmd)}")65 subprocess.run(cmd)66 return True67 68 except Exception as e:69 print(f"โ Error launching Streamlit: {e}")70 return False71 72def launch_dash():73 """Launch Dash application"""74 print("๐ Launching Dash UI...")75 76 # Create dash app file if it doesn't exist77 dash_app_path = "ui/dash_app.py"78 if not os.path.exists(dash_app_path):79 print(f"โ Dash app not found at {dash_app_path}")80 return False81 82 try:83 # Launch Dash84 cmd = [85 sys.executable, dash_app_path86 ]87 88 print(f"Running: {' '.join(cmd)}")89 subprocess.run(cmd)90 return True91 92 except Exception as e:93 print(f"โ Error launching Dash: {e}")94 return False95 96def launch_jupyter():97 """Launch Jupyter interface"""98 print("๐ Launching Jupyter UI...")99 100 try:101 # Launch Jupyter Lab102 cmd = [103 sys.executable, "-m", "jupyter", "lab",104 "--port", "8888",105 "--ip", "0.0.0.0",106 "--no-browser"107 ]108 109 print(f"Running: {' '.join(cmd)}")110 subprocess.run(cmd)111 return True112 113 except Exception as e:114 print(f"โ Error launching Jupyter: {e}")115 return False116 117def launch_websocket_server():118 """Launch WebSocket server"""119 print("๐ Launching WebSocket Server...")120 121 try:122 from ui.websocket_server import create_websocket_server123 124 server = create_websocket_server(host="0.0.0.0", port=8765)125 server_thread = server.run_server()126 127 print("โ
WebSocket server started on ws://0.0.0.0:8765")128 print("Press Ctrl+C to stop the server")129 130 # Keep the main thread alive131 try:132 while True:133 time.sleep(1)134 except KeyboardInterrupt:135 print("\n๐ Stopping WebSocket server...")136 137 return True138 139 except Exception as e:140 print(f"โ Error launching WebSocket server: {e}")141 return False142 143def open_browser(url: str, delay: int = 2):144 """Open browser after delay"""145 def open_url():146 time.sleep(delay)147 try:148 webbrowser.open(url)149 print(f"๐ Opened browser to: {url}")150 except Exception as e:151 print(f"โ ๏ธ Could not open browser: {e}")152 153 browser_thread = threading.Thread(target=open_url)154 browser_thread.daemon = True155 browser_thread.start()156 157def main():158 """Main launcher function"""159 parser = argparse.ArgumentParser(160 description="UI Launcher for Algorithmic Trading System",161 formatter_class=argparse.RawDescriptionHelpFormatter,162 epilog="""163Examples:164 python ui_launcher.py streamlit # Launch Streamlit UI165 python ui_launcher.py dash # Launch Dash UI166 python ui_launcher.py jupyter # Launch Jupyter Lab167 python ui_launcher.py websocket # Launch WebSocket server168 python ui_launcher.py all # Launch all UIs169 """170 )171 172 parser.add_argument(173 "ui_type",174 choices=["streamlit", "dash", "jupyter", "websocket", "all"],175 help="Type of UI to launch"176 )177 178 parser.add_argument(179 "--no-browser",180 action="store_true",181 help="Don't automatically open browser"182 )183 184 parser.add_argument(185 "--port",186 type=int,187 help="Custom port number (overrides default)"188 )189 190 args = parser.parse_args()191 192 # Check dependencies193 if not check_dependencies():194 sys.exit(1)195 196 print("๐ค Algorithmic Trading System - UI Launcher")197 print("=" * 50)198 199 success = False200 201 if args.ui_type == "streamlit":202 success = launch_streamlit()203 if success and not args.no_browser:204 open_browser("http://localhost:8501")205 206 elif args.ui_type == "dash":207 success = launch_dash()208 if success and not args.no_browser:209 open_browser("http://localhost:8050")210 211 elif args.ui_type == "jupyter":212 success = launch_jupyter()213 if success and not args.no_browser:214 open_browser("http://localhost:8888")215 216 elif args.ui_type == "websocket":217 success = launch_websocket_server()218 219 elif args.ui_type == "all":220 print("๐ Launching all UI interfaces...")221 222 # Launch WebSocket server in background223 websocket_thread = threading.Thread(target=launch_websocket_server)224 websocket_thread.daemon = True225 websocket_thread.start()226 227 # Launch Streamlit228 streamlit_thread = threading.Thread(target=launch_streamlit)229 streamlit_thread.daemon = True230 streamlit_thread.start()231 232 # Launch Dash233 dash_thread = threading.Thread(target=launch_dash)234 dash_thread.daemon = True235 dash_thread.start()236 237 # Launch Jupyter238 jupyter_thread = threading.Thread(target=launch_jupyter)239 jupyter_thread.daemon = True240 jupyter_thread.start()241 242 if not args.no_browser:243 open_browser("http://localhost:8501", 3) # Streamlit244 open_browser("http://localhost:8050", 5) # Dash245 open_browser("http://localhost:8888", 7) # Jupyter246 247 print("โ
All UIs launched!")248 print("๐ Streamlit: http://localhost:8501")249 print("๐ Dash: http://localhost:8050")250 print("๐ Jupyter: http://localhost:8888")251 print("๐ WebSocket: ws://localhost:8765")252 253 # Keep main thread alive254 try:255 while True:256 time.sleep(1)257 except KeyboardInterrupt:258 print("\n๐ Stopping all UIs...")259 260 success = True261 262 if success:263 print("โ
UI launched successfully!")264 else:265 print("โ Failed to launch UI")266 sys.exit(1)267 268if __name__ == "__main__":269 main() 