分步式页面布局,首页页面设计实现初稿

This commit is contained in:
YueL1331
2026-01-08 13:53:19 +08:00
parent af4b4a28c3
commit a5b0b71d26
20 changed files with 1749 additions and 569 deletions

65
2.1版本/app.py Normal file
View File

@ -0,0 +1,65 @@
import os
import sys
from flask import Flask, jsonify
from flask_cors import CORS
from models import db, Device, DeviceHistory, MaintenanceLog
from routes.api import api_bp # 从 api.py 导入蓝图
# 解决 Windows 下控制台输出乱码问题
sys.stdout.reconfigure(encoding='utf-8')
def create_app():
app = Flask(__name__)
# 1. 配置数据库路径
basedir = os.path.abspath(os.path.dirname(__file__))
# 👇👇👇 核心修复:自动创建 instance 文件夹 👇👇👇
instance_path = os.path.join(basedir, 'instance')
if not os.path.exists(instance_path):
os.makedirs(instance_path)
print(f"📁 检测到目录不存在,已自动创建: {instance_path}")
# 👆👆👆 修复结束 👆👆👆
db_path = os.path.join(instance_path, 'devices.db')
# 配置 SQLite URI
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['JSON_AS_ASCII'] = False # 支持中文返回
# 2. 初始化插件
CORS(app) # 允许跨域
db.init_app(app)
# 3. 注册蓝图 (Blueprints)
app.register_blueprint(api_bp)
# 4. 初始化数据库表
with app.app_context():
# 尝试创建所有表
db.create_all()
# print(f"✅ 数据库连接成功: {db_path}")
return app
# 5. 提供 Flask Shell 上下文(方便命令行调试)
app = create_app()
@app.shell_context_processor
def make_shell_context():
return {
'db': db,
'Device': Device,
'DeviceHistory': DeviceHistory,
'MaintenanceLog': MaintenanceLog
}
if __name__ == '__main__':
# 启动应用
print("🚀 服务正在启动: http://127.0.0.1:5000")
app.run(debug=True, host='0.0.0.0', port=5000)