diff --git a/run_web_v2.sh b/run_web_v2.sh new file mode 100644 index 00000000..cad618fa --- /dev/null +++ b/run_web_v2.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# LED Matrix Web Interface V2 Runner +# This script sets up a virtual environment and runs the web interface + +set -e + +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +echo "Setting up LED Matrix Web Interface V2..." + +# Check if virtual environment exists +if [ ! -d "venv_web_v2" ]; then + echo "Creating virtual environment..." + python3 -m venv venv_web_v2 +fi + +# Activate virtual environment +echo "Activating virtual environment..." +source venv_web_v2/bin/activate + +# Install dependencies +echo "Installing dependencies..." +pip install -r requirements_web_v2.txt + +# Run the web interface +echo "Starting web interface on http://0.0.0.0:5001" +python web_interface_v2.py \ No newline at end of file diff --git a/run_web_v2_simple.py b/run_web_v2_simple.py new file mode 100644 index 00000000..d6c3630e --- /dev/null +++ b/run_web_v2_simple.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +""" +Simple runner for LED Matrix Web Interface V2 +Handles virtual environment setup and dependency installation +""" + +import os +import sys +import subprocess +import logging +from pathlib import Path + +# Setup logging +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s' +) + +logger = logging.getLogger(__name__) + +def main(): + """Main function to set up and run the web interface.""" + # Change to script directory + script_dir = Path(__file__).parent + os.chdir(script_dir) + + venv_path = script_dir / 'venv_web_v2' + + # Create virtual environment if it doesn't exist + if not venv_path.exists(): + logger.info("Creating virtual environment...") + subprocess.check_call([ + sys.executable, '-m', 'venv', str(venv_path) + ]) + logger.info("Virtual environment created successfully") + + # Get virtual environment Python and pip paths + if os.name == 'nt': # Windows + venv_python = venv_path / 'Scripts' / 'python.exe' + venv_pip = venv_path / 'Scripts' / 'pip.exe' + else: # Unix/Linux + venv_python = venv_path / 'bin' / 'python' + venv_pip = venv_path / 'bin' / 'pip' + + # Install dependencies + logger.info("Installing dependencies...") + subprocess.check_call([ + str(venv_pip), 'install', '-r', 'requirements_web_v2.txt' + ]) + + # Run the web interface + logger.info("Starting web interface on http://0.0.0.0:5001") + subprocess.run([str(venv_python), 'web_interface_v2.py']) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/start_web_v2.py b/start_web_v2.py index cc25a7f9..87ca2dd6 100755 --- a/start_web_v2.py +++ b/start_web_v2.py @@ -22,8 +22,39 @@ logging.basicConfig( logger = logging.getLogger(__name__) -def check_dependencies(): - """Check if required dependencies are installed.""" +def setup_virtual_environment(): + """Set up a virtual environment for the web interface.""" + venv_path = Path(__file__).parent / 'venv_web_v2' + + if not venv_path.exists(): + logger.info("Creating virtual environment...") + try: + subprocess.check_call([ + sys.executable, '-m', 'venv', str(venv_path) + ]) + logger.info("Virtual environment created successfully") + except subprocess.CalledProcessError as e: + logger.error(f"Failed to create virtual environment: {e}") + return None + + return venv_path + +def get_venv_python(venv_path): + """Get the Python executable path from the virtual environment.""" + if os.name == 'nt': # Windows + return venv_path / 'Scripts' / 'python.exe' + else: # Unix/Linux + return venv_path / 'bin' / 'python' + +def get_venv_pip(venv_path): + """Get the pip executable path from the virtual environment.""" + if os.name == 'nt': # Windows + return venv_path / 'Scripts' / 'pip.exe' + else: # Unix/Linux + return venv_path / 'bin' / 'pip' + +def check_dependencies(venv_path): + """Check if required dependencies are installed in the virtual environment.""" required_packages = [ 'flask', 'flask_socketio', @@ -32,19 +63,25 @@ def check_dependencies(): 'eventlet' ] + # Use the virtual environment's Python to check imports + venv_python = get_venv_python(venv_path) + missing_packages = [] for package in required_packages: try: - __import__(package) - except ImportError: + subprocess.check_call([ + str(venv_python), '-c', f'import {package}' + ], capture_output=True) + except subprocess.CalledProcessError: missing_packages.append(package) if missing_packages: logger.warning(f"Missing packages: {missing_packages}") - logger.info("Installing missing packages...") + logger.info("Installing missing packages in virtual environment...") try: + venv_pip = get_venv_pip(venv_path) subprocess.check_call([ - sys.executable, '-m', 'pip', 'install', '-r', 'requirements_web_v2.txt' + str(venv_pip), 'install', '-r', 'requirements_web_v2.txt' ]) logger.info("Dependencies installed successfully") except subprocess.CalledProcessError as e: @@ -74,32 +111,31 @@ def main(): script_dir = Path(__file__).parent os.chdir(script_dir) - # Check dependencies - if not check_dependencies(): + # Set up virtual environment + venv_path = setup_virtual_environment() + if not venv_path: + logger.error("Failed to set up virtual environment. Exiting.") + sys.exit(1) + + # Check dependencies in virtual environment + if not check_dependencies(venv_path): logger.error("Dependency check failed. Exiting.") sys.exit(1) # Check permissions check_permissions() - # Import and start the web interface + # Import and start the web interface using the virtual environment's Python try: - from web_interface_v2 import app, socketio + venv_python = get_venv_python(venv_path) logger.info("Web interface loaded successfully") - # Start the server + # Start the server using the virtual environment's Python logger.info("Starting web server on http://0.0.0.0:5001") - socketio.run( - app, - host='0.0.0.0', - port=5001, # Use port 5001 to avoid conflicts - debug=False, - allow_unsafe_werkzeug=True - ) + subprocess.run([ + str(venv_python), 'web_interface_v2.py' + ]) - except ImportError as e: - logger.error(f"Failed to import web interface: {e}") - sys.exit(1) except Exception as e: logger.error(f"Failed to start web interface: {e}") sys.exit(1) diff --git a/web_interface_v2.py b/web_interface_v2.py index f58d8dd9..5c376ce1 100644 --- a/web_interface_v2.py +++ b/web_interface_v2.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, send_file from flask_socketio import SocketIO, emit import json @@ -447,4 +448,4 @@ if __name__ == '__main__': display_monitor.start() # Run the app - socketio.run(app, host='0.0.0.0', port=5000, debug=False) \ No newline at end of file + socketio.run(app, host='0.0.0.0', port=5001, debug=False, allow_unsafe_werkzeug=True) \ No newline at end of file