源码下载:https://download.csdn.net/download/ayangann915/12325618
base_setting.py:是默认的设置信息,一般存放公共的,如端口号等
SERVER_PORT = 5000
DEBUG = False
SQLALCHEMY_ECHO = False
local_setting.py:存放开发时需要的信息,如数据库,开发时debug为True
SQLALCHEMY_DATABASE_URI = "mysql://root:[email protected]/user_grade"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = True
DEBUG=True
SQLALCHEMY_ENCODING = 'utf-8'
production_setting.py:实际应用,生产环境
这时debug需要设置为false
DEBUG=False
可以在终端set使用哪个setting文件:
if 'ops_config' in os.environ:
self.config.from_pyfile('config/%s_setting.py' % (os.environ['ops_config']))
运行时:
export ops_config=local#相当于指定使用local_setting文件
application.py:封装变量等
from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
import os
class Application(Flask):
def __init__(self, import_name):
super(Application, self).__init__(import_name)
self.config.from_pyfile('config/base_setting.py')
if 'ops_config' in os.environ:
self.config.from_pyfile('config/%s_setting.py' % (os.environ['ops_config']))
db.init_app(self)
"""
export ops_config=local
一般base是公用的,如端口号配置,数据库配置在local中
"""
db = SQLAlchemy()
app = Application(__name__)
manager = Manager(app)
#app = Flask(__name__)
www.py:模块注册
from web.controllers.index import route_index
from application import app
app.register_blueprint(route_index, url_prefix="/")
manager.py:启动入口
from application import app, manager
from flask_script import Server
import www
#自定义命令
manager.add_command("runserver", Server(host="0.0.0.0",port=app.config['SERVER_PORT'],use_debugger=True,use_reloader=None))
def main():
manager.run()
if __name__ == '__main__':
try:
import sys
sys.exit(main())
except Exception as e:
import traceback
traceback.print_exc(e)
自定义server命令:
运行时输入:
python manager.py runserver