# PowerShell Script: Start Flask API Server # Use virtual environment to run flask_api.py # Set console encoding to UTF-8 chcp 65001 | Out-Null [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 Write-Host "========================================" -ForegroundColor Cyan Write-Host "Starting Flask API Server" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" # Change to script directory $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path Set-Location $scriptPath Write-Host "Working directory: $scriptPath" -ForegroundColor Gray Write-Host "" # Check if virtual environment exists $venvPath = Join-Path $scriptPath ".venv" if (-not (Test-Path $venvPath)) { Write-Host "[ERROR] Virtual environment not found (.venv directory missing)" -ForegroundColor Red Write-Host "Please run setup_venv.bat first to create virtual environment" -ForegroundColor Yellow Write-Host "" Read-Host "Press Enter to exit" exit 1 } # Check if activation script exists $activateScript = Join-Path $venvPath "Scripts\Activate.ps1" if (-not (Test-Path $activateScript)) { Write-Host "[ERROR] Virtual environment activation script not found" -ForegroundColor Red Write-Host "Path: $activateScript" -ForegroundColor Yellow Write-Host "Please recreate the virtual environment" -ForegroundColor Yellow Write-Host "" Read-Host "Press Enter to exit" exit 1 } # Check if flask_api.py exists $flaskApiFile = Join-Path $scriptPath "flask_api.py" if (-not (Test-Path $flaskApiFile)) { Write-Host "[ERROR] flask_api.py file not found" -ForegroundColor Red Write-Host "Path: $flaskApiFile" -ForegroundColor Yellow Write-Host "" Read-Host "Press Enter to exit" exit 1 } Write-Host "Activating virtual environment..." -ForegroundColor Green try { # Activate virtual environment & $activateScript Write-Host "Virtual environment activated" -ForegroundColor Green Write-Host "" } catch { Write-Host "[ERROR] Error activating virtual environment: $_" -ForegroundColor Red Read-Host "Press Enter to exit" exit 1 } # Check if Python is available Write-Host "Checking Python environment..." -ForegroundColor Green $pythonPath = Join-Path $venvPath "Scripts\python.exe" if (-not (Test-Path $pythonPath)) { Write-Host "[ERROR] Python interpreter not found: $pythonPath" -ForegroundColor Red Read-Host "Press Enter to exit" exit 1 } # Display Python version $pythonVersion = & $pythonPath --version 2>&1 Write-Host "Python version: $pythonVersion" -ForegroundColor Gray Write-Host "" # Start Flask API server Write-Host "========================================" -ForegroundColor Cyan Write-Host "Starting Flask API Server..." -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "Server will start at http://localhost:5050" -ForegroundColor Yellow Write-Host "Press Ctrl+C to stop the server" -ForegroundColor Yellow Write-Host "" try { # Run flask_api.py using Python from virtual environment & $pythonPath flask_api.py } catch { Write-Host "" Write-Host "[ERROR] Error running Flask API: $_" -ForegroundColor Red Read-Host "Press Enter to exit" exit 1 } finally { Write-Host "" Write-Host "Server stopped" -ForegroundColor Gray }