first commit
This commit is contained in:
108
backend/python_flask.py
Normal file
108
backend/python_flask.py
Normal file
@@ -0,0 +1,108 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Python Flask 后端项目初始化模块
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def run_command_with_progress(cmd, cwd, description):
|
||||
"""运行命令并显示实时输出"""
|
||||
try:
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"执行: {description}")
|
||||
print(f"{'=' * 60}")
|
||||
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
cwd=cwd,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
bufsize=0
|
||||
)
|
||||
|
||||
for line in process.stdout:
|
||||
try:
|
||||
decoded_line = line.decode('utf-8', errors='replace')
|
||||
except:
|
||||
try:
|
||||
decoded_line = line.decode('gbk', errors='replace')
|
||||
except:
|
||||
decoded_line = line.decode('latin-1', errors='replace')
|
||||
print(decoded_line, end='')
|
||||
|
||||
process.wait()
|
||||
|
||||
if process.returncode == 0:
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"✅ {description} - 完成")
|
||||
print(f"{'=' * 60}\n")
|
||||
return True
|
||||
else:
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"❌ {description} - 失败 (错误码: {process.returncode})")
|
||||
print(f"{'=' * 60}\n")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 执行失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def init_flask_project(backend_dir, project_name):
|
||||
"""初始化 Flask 项目"""
|
||||
print("\n🚀 初始化 Flask 项目...")
|
||||
|
||||
# 创建 app.py
|
||||
app_py = backend_dir / "app.py"
|
||||
app_py.write_text(f'''from flask import Flask, jsonify
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app) # 允许跨域
|
||||
|
||||
@app.route('/api/health', methods=['GET'])
|
||||
def health():
|
||||
return jsonify({{
|
||||
'status': 'ok',
|
||||
'message': 'Welcome to {project_name} API'
|
||||
}})
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("🚀 服务器启动: http://localhost:8080")
|
||||
print("📍 健康检查: http://localhost:8080/api/health")
|
||||
app.run(debug=True, host='0.0.0.0', port=8080)
|
||||
''', encoding='utf-8')
|
||||
|
||||
# 创建 requirements.txt
|
||||
requirements = backend_dir / "requirements.txt"
|
||||
requirements.write_text('''Flask==3.0.0
|
||||
flask-cors==4.0.0
|
||||
''', encoding='utf-8')
|
||||
|
||||
# 创建虚拟环境并安装依赖
|
||||
success = run_command_with_progress(
|
||||
f'"{sys.executable}" -m venv venv',
|
||||
backend_dir,
|
||||
"创建 Python 虚拟环境"
|
||||
)
|
||||
|
||||
if success:
|
||||
venv_python = backend_dir / "venv" / "Scripts" / "python.exe"
|
||||
if venv_python.exists():
|
||||
success = run_command_with_progress(
|
||||
f'"{venv_python}" -m pip install -r requirements.txt',
|
||||
backend_dir,
|
||||
"安装 Flask 依赖"
|
||||
)
|
||||
|
||||
if success:
|
||||
print("\n✅ Flask 项目初始化成功")
|
||||
print("💡 启动命令: venv\\Scripts\\python app.py")
|
||||
else:
|
||||
print("\n⚠️ Flask 项目创建成功,但依赖安装失败")
|
||||
print(f" 请手动运行: cd {project_name}-backend && venv\\Scripts\\pip install -r requirements.txt")
|
||||
else:
|
||||
print("\n⚠️ 虚拟环境创建失败,请检查 Python 安装")
|
||||
Reference in New Issue
Block a user