From 047cafa7c701ce264f2967cb51cf8452b32ba377 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Thu, 28 Nov 2024 11:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 14 ++++---- docs/release_guide.md | 30 +++++++++++++++++ docs/windows_setup.md | 76 +++++++++++++++++++++++++++++++++++++++++++ scripts/build_win.ps1 | 76 +++++++++++++++++++++++++++++++++++++++++++ src/start.bat | 9 +++++ 5 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 docs/release_guide.md create mode 100644 docs/windows_setup.md create mode 100644 scripts/build_win.ps1 create mode 100644 src/start.bat diff --git a/config.py b/config.py index fa09fcf..32d65a6 100644 --- a/config.py +++ b/config.py @@ -3,15 +3,15 @@ import os class Config: """配置类""" # 数据库配置 - MYSQL_HOST = 'localhost' - MYSQL_USER = 'root' - MYSQL_PASSWORD = '123456' - MYSQL_DB = 'equipment_cost_db' + MYSQL_HOST = os.getenv('MYSQL_HOST', 'localhost') + MYSQL_USER = os.getenv('MYSQL_USER', 'root') + MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', '123456') + MYSQL_DB = os.getenv('MYSQL_DB', 'equipment_cost_db') # Flask配置 FLASK_HOST = '0.0.0.0' FLASK_PORT = 5001 - FLASK_DEBUG = True + FLASK_DEBUG = os.getenv('FLASK_DEBUG', 'True').lower() == 'true' # 目录配置 MODEL_DIR = 'models' @@ -52,8 +52,8 @@ class Config: CACHE_DEFAULT_TIMEOUT = 300 # 安全配置 - SECRET_KEY = 'your-secret-key-here' - JWT_SECRET_KEY = 'your-jwt-secret-key-here' + SECRET_KEY = os.getenv('SECRET_KEY', 'your-secret-key-here') + JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY', 'your-jwt-secret-key-here') JWT_ACCESS_TOKEN_EXPIRES = 3600 # 1小时 # 跨域配置 diff --git a/docs/release_guide.md b/docs/release_guide.md new file mode 100644 index 0000000..053cdc6 --- /dev/null +++ b/docs/release_guide.md @@ -0,0 +1,30 @@ +# Windows 发布包制作指南 + +1. 准备工作 + +1.1 安装必要软件 + +- Python 3.11.8: +- Visual Studio Build Tools: + +1.2 下载安装程序 + +- Python 3.11.8: +- Visual C++ Redistributable: + + +1. 克隆项目 + +```powershell +git clone [repository-url] +cd cost-prediction +``` + +2. 打包步骤 + +```powershell +# 运行打包脚本 +.\scripts\build_win.ps1 +``` + +打包完成后会在项目根目录生成 `cost-prediction-[version]-win64.zip`。 diff --git a/docs/windows_setup.md b/docs/windows_setup.md new file mode 100644 index 0000000..9d113b3 --- /dev/null +++ b/docs/windows_setup.md @@ -0,0 +1,76 @@ +# Windows 开发环境设置 + +1. 安装必要软件 + - Python 3.11.8: + - Git: + - MySQL 8.0+: + - Visual Studio Build Tools: + - Node.js 18+ LTS: ,安装时,Chocolatey不是必需的 + - npm 9+: (随 Node.js 一起安装) + +2. 克隆项目 + +```powershell +git clone [repository-url] +cd cost-prediction +``` + +3. 设置前端环境 + +```powershell +# 进入前端目录 +cd frontend + +# 安装依赖 +npm install + +# 构建生产版本 +npm run build + +# 返回项目根目录 +cd .. +``` + +4. 设置 Python 环境 + +```powershell +# 创建虚拟环境 +python -m venv .venv + +# 激活虚拟环境 +.\.venv\Scripts\Activate.ps1 + +# 安装依赖 +pip install -e . +``` + +5. 配置数据库 + +```powershell +# 确保 MySQL 服务已启动 +net start MySQL80 + +# 初始化数据库和导入数据 +.\scripts\init_db.ps1 + +# 运行数据库配置脚本 +.\scripts\setup_env.ps1 +``` + +6. 运行测试 + +```powershell +python src/test_api.py +``` + +7. 打包项目 + +```powershell +# 先下载所有依赖 +.\scripts\download_deps.ps1 + +# 然后运行打包脚本 +.\scripts\build_win.ps1 +``` + +## 注意:如果需要制作发布包,请参考 docs/release_guide.md diff --git a/scripts/build_win.ps1 b/scripts/build_win.ps1 new file mode 100644 index 0000000..83a0894 --- /dev/null +++ b/scripts/build_win.ps1 @@ -0,0 +1,76 @@ +# Set console encoding to UTF-8 +[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 +$OutputEncoding = [System.Text.Encoding]::UTF8 + +# Ensure PowerShell stops on error +$ErrorActionPreference = "Stop" + +# Create virtual environment +Write-Host "Creating virtual environment..." +python -m venv .venv +.\.venv\Scripts\Activate.ps1 + +# Install dependencies +Write-Host "Installing dependencies..." +pip install -e . + +# Build frontend +Write-Host "Building frontend..." +Push-Location frontend +npm install +npm run build +Pop-Location + +# Package with PyInstaller +Write-Host "Starting packaging..." +# Create necessary directories +New-Item -ItemType Directory -Force -Path "logs" +New-Item -ItemType Directory -Force -Path "data" +New-Item -ItemType Directory -Force -Path "models" + +pyinstaller --clean ` + --add-data "src/loitering_munition_data.sql;data" ` + --add-data "src/rocket_artillery_data.sql;data" ` + --add-data "src/manufacturer_data.sql;data" ` + --add-data "src/schema.sql;data" ` + --add-data "config.py;." ` + --add-data "src;src" ` + --add-data "frontend;frontend" ` + --add-data "logs;logs" ` + --add-data "data;data" ` + --add-data "models;models" ` + --collect-all "xgboost" ` + --collect-all "lightgbm" ` + --collect-all "torch" ` + --collect-all "sklearn" ` + --collect-all "numpy" ` + --collect-all "pandas" ` + --collect-all "sqlalchemy" ` + --collect-all "pymysql" ` + --collect-all "cryptography" ` + --collect-all "flask" ` + --collect-all "flask_cors" ` + --hidden-import "xgboost.testing" ` + --hidden-import "torch.utils.tensorboard" ` + --hidden-import "pytest" ` + run.py + +# Copy necessary files +Write-Host "Copying configuration files..." +Copy-Item "src/start.bat" -Destination "dist/run" + +# Create release package +Write-Host "Creating release package..." +$version = (Get-Content pyproject.toml | Select-String 'version = "(.*?)"').Matches.Groups[1].Value + +# Create complete offline installation package directory +$RELEASE_DIR = "dist/release" +New-Item -ItemType Directory -Force -Path $RELEASE_DIR + +# Copy application files +Copy-Item "dist/run/*" -Destination $RELEASE_DIR -Recurse + +# Create final zip package +Compress-Archive -Path "$RELEASE_DIR/*" -DestinationPath "cost-prediction-$version-win64.zip" -Force + +Write-Host "Package completed: cost-prediction-$version-win64.zip" \ No newline at end of file diff --git a/src/start.bat b/src/start.bat new file mode 100644 index 0000000..6443a68 --- /dev/null +++ b/src/start.bat @@ -0,0 +1,9 @@ +@echo off +set FLASK_DEBUG=false +set MYSQL_HOST=localhost +set MYSQL_USER=root +set MYSQL_PASSWORD=123456 +set MYSQL_DB=equipment_cost_db +echo Starting Cost Prediction System... +start /B run.exe +start http://localhost:5001 \ No newline at end of file