从flask script 转换到flask cli

#flask-script -> flask-Cli
./manage.py runserver -> flask run
./manage.py shell ->flask shell

命令转换

通过cli运行:要设置环境变量 export FLASK_APP=flask.py
flask run 

原本的上下文:
def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment)
manager.add_command("shell", Shell(make_context=make_shell_context))
cli后的:
@app.shell_context_processor
def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment)
用装饰器装饰


自定义函数命令行扩展:
@app.cli.command()
@click.option('--coverage/--no-coverage', default=False, help='Enable code coverage')
def test(coverage):
    """Run the unit tests."""
    # ...
用app.cli.command()装饰
可用flask test 在命令行中运行test()函数。


猜你喜欢

转载自blog.csdn.net/CSDN_Gjx/article/details/79301951
今日推荐