start_flask_api.ps1 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # PowerShell Script: Start Flask API Server
  2. # Use virtual environment to run flask_api.py
  3. # Set console encoding to UTF-8
  4. chcp 65001 | Out-Null
  5. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  6. $OutputEncoding = [System.Text.Encoding]::UTF8
  7. Write-Host "========================================" -ForegroundColor Cyan
  8. Write-Host "Starting Flask API Server" -ForegroundColor Cyan
  9. Write-Host "========================================" -ForegroundColor Cyan
  10. Write-Host ""
  11. # Change to script directory
  12. $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
  13. Set-Location $scriptPath
  14. Write-Host "Working directory: $scriptPath" -ForegroundColor Gray
  15. Write-Host ""
  16. # Check if virtual environment exists
  17. $venvPath = Join-Path $scriptPath ".venv"
  18. if (-not (Test-Path $venvPath)) {
  19. Write-Host "[ERROR] Virtual environment not found (.venv directory missing)" -ForegroundColor Red
  20. Write-Host "Please run setup_venv.bat first to create virtual environment" -ForegroundColor Yellow
  21. Write-Host ""
  22. Read-Host "Press Enter to exit"
  23. exit 1
  24. }
  25. # Check if activation script exists
  26. $activateScript = Join-Path $venvPath "Scripts\Activate.ps1"
  27. if (-not (Test-Path $activateScript)) {
  28. Write-Host "[ERROR] Virtual environment activation script not found" -ForegroundColor Red
  29. Write-Host "Path: $activateScript" -ForegroundColor Yellow
  30. Write-Host "Please recreate the virtual environment" -ForegroundColor Yellow
  31. Write-Host ""
  32. Read-Host "Press Enter to exit"
  33. exit 1
  34. }
  35. # Check if flask_api.py exists
  36. $flaskApiFile = Join-Path $scriptPath "flask_api.py"
  37. if (-not (Test-Path $flaskApiFile)) {
  38. Write-Host "[ERROR] flask_api.py file not found" -ForegroundColor Red
  39. Write-Host "Path: $flaskApiFile" -ForegroundColor Yellow
  40. Write-Host ""
  41. Read-Host "Press Enter to exit"
  42. exit 1
  43. }
  44. Write-Host "Activating virtual environment..." -ForegroundColor Green
  45. try {
  46. # Activate virtual environment
  47. & $activateScript
  48. Write-Host "Virtual environment activated" -ForegroundColor Green
  49. Write-Host ""
  50. } catch {
  51. Write-Host "[ERROR] Error activating virtual environment: $_" -ForegroundColor Red
  52. Read-Host "Press Enter to exit"
  53. exit 1
  54. }
  55. # Check if Python is available
  56. Write-Host "Checking Python environment..." -ForegroundColor Green
  57. $pythonPath = Join-Path $venvPath "Scripts\python.exe"
  58. if (-not (Test-Path $pythonPath)) {
  59. Write-Host "[ERROR] Python interpreter not found: $pythonPath" -ForegroundColor Red
  60. Read-Host "Press Enter to exit"
  61. exit 1
  62. }
  63. # Display Python version
  64. $pythonVersion = & $pythonPath --version 2>&1
  65. Write-Host "Python version: $pythonVersion" -ForegroundColor Gray
  66. Write-Host ""
  67. # Start Flask API server
  68. Write-Host "========================================" -ForegroundColor Cyan
  69. Write-Host "Starting Flask API Server..." -ForegroundColor Cyan
  70. Write-Host "========================================" -ForegroundColor Cyan
  71. Write-Host ""
  72. Write-Host "Server will start at http://localhost:5050" -ForegroundColor Yellow
  73. Write-Host "Press Ctrl+C to stop the server" -ForegroundColor Yellow
  74. Write-Host ""
  75. try {
  76. # Run flask_api.py using Python from virtual environment
  77. & $pythonPath flask_api.py
  78. } catch {
  79. Write-Host ""
  80. Write-Host "[ERROR] Error running Flask API: $_" -ForegroundColor Red
  81. Read-Host "Press Enter to exit"
  82. exit 1
  83. } finally {
  84. Write-Host ""
  85. Write-Host "Server stopped" -ForegroundColor Gray
  86. }