flask_script
flask_script? 1.????? flask_scriptdoc: https://flask-script.readthedocs.io/en/latest/ ? The Flask-Script extension provides support for writing external scripts in Flask. This includes running a development server,a customised Python shell,scripts to set up your database,cronjobs,and other command-line tasks that belong outside the web application itself. flask-script扩展提供了在flask上添加执行外围脚本的功能,包括运行开发服务器、定制命令、创建数据库、定时任务、或者其它命令行任务,上述内容均独立于flask应用本身。 Flask-Script works in a similar way to Flask itself. You define and add commands that can be called from the command line to a Manager instance: 换句话说它提供了一个包装,可以自定义命令,在命令行中调用Manager实例。 ? 1.1.??? install/uninstallpip install flask_script pip uninstall flask_script ? 1.2.??? 添加命令常用的有两种方式,装饰器,子类: # 装饰器添加命令 @manager.command def hello_1(): ??? print(‘hello 1‘) ? # 类方式添加命令 from flask_script import Command class hello_2(Command): ??? "prints hello world" ? ??? def run(self): ??????? print("hello 2") ? manager.add_command(‘hello_2‘,hello_2) ? 其实还有一种option方式,用于复杂命令,有需要再看,这里不再赘述。 ? 1.3.??? 案例from app import create_app import flask_migrate from flask_script import Manager ? ? app = create_app() manager = Manager(app) ? # 装饰器添加命令 @manager.command def hello_1(): ??? print(‘hello 1‘) ? # 类方式添加命令 from flask_script import Command class hello_2(Command): ??? "prints hello world" ? ??? def run(self): ??????? print("hello 2") ? manager.add_command(‘hello_2‘,hello_2) ? if __name__ == ‘__main__‘: ??? manager.run() ??? 结果释义: 执行命令 python manage.py hello_1 # hello_1 运行正常 python manage.py hello_2 # hello_2 运行正常 python manage.py hello_3 # manage.py: error: invalid choice: ?运行异常,因为命令未声明 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |