其一Flask_Script
安装: pip3 install flask_script
flask_script作用:
-改变flask的启动方式:python manage.py runserver
-自定义命令: python manage.py 自定义命令
示例:
manege.py
from flask_script import Manager
from flaskscript import create_app
app=create_app()
manager=Manager(app)
@manager.command #执行命令 python manage.py custom 123
def custom(arg):
"""
自定义命令
python manage.py custom 123
:param arg:
:return:
"""
print(arg)
@manager.option('-n', '--name', dest='name')
@manager.option('-u', '--url', dest='url')
def cmd(name, url):
"""
自定义命令
执行: python manage.py cmd -n wupeiqi -u http://www.oldboyedu.com
:param name:
:param url:
:return:
"""
print(name, url)
if __name__=="__main__":
manager.run()
其二 flask_sqlalchemy
作用:将SQLAlchemy相关的所有功能都封装到db=flask_sqlalchemy.SQLAlchemy()对象中
- 创建表
class User(db.Model):
pass
- 操作表
db.session
其三 flask_migrate
作用:做数据库迁移
依赖:
flask-script
flask-sqlalchemy
Migrate(app, db)
"""
# 数据库迁移命令
python manage.py db init
python manage.py db migrate # makemigrations
python manage.py db upgrade # migrate
"""
manager.add_command('db', MigrateCommand)
其四 多app应用
#对url进行处理和分发
from flask import Flask
from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
app01 = Flask('app01')
app02 = Flask('app02')
@app01.route('/login')
def login():
return 'app01.login'
@app02.route('/index')
def index():
return 'app02.index'
# 访问"login"--->"http://localhost:5000/login"
# 访问"index"--->"http://localhost:5000/app02/index"
dm = DispatcherMiddleware(app01, {
'/app02': app02,
})
if __name__ == '__main__':
run_simple('localhost', 5000, dm)