Flask-Script通过命令行的形式来操作Flask
文档:https://flask-script.readthedocs.io/en/latest/ Github: https://github.com/smurfix/flask-script
该项目文档中说,Flask从0.11之后开始自带命令行工具,此项目不再添加新功能,仅维护状态
安装pip install Flask-Script
使用示例
manage.py
# -*- coding: utf-8 -*-
# app
from flask import Flask
app = Flask(__name__)
# manager
from flask_script import Manager, Command
manager = Manager(app)
# 1、添加不需要传递参数的命令
@manager.command
def hello():
print("hello")
# 2、添加需要传递参数的命令
@manager.option('-n', '--name', dest='name')
def say_name(name):
print(name)
# 3、调用方法添加
class Print(Command):
def run(self):
print('Print')
manager.add_command('print', Print())
if __name__ == "__main__":
manager.run()
使用测试
$ python manage.py hello
hello
$ python manage.py say_name -n Tom
Tom
$ python manage.py print
Print
参考 flask-script的基本使用