【FLASK】Flask-Script

慕逸仙
2023-12-01

Flask Script扩展提供向Flask插入外部脚本的功能

Manager

Manager可以看作是一个监视命令行的脚本,该脚本同时也可以添加命令,有三种方法添加命令:

1.创建Command子类,子类必须定义一个run方法

from flask_script import Manager
from flask_script import Command
from debug import app

manager = Manager(app)

class Hello(Command):
    'hello world'
    def run(self):
        print('hello world')

manager.add_command('hello',Hello())
if __name__ == "__main__":
    manager.run()

结果:
python manager.py hello
> hello world

2.使用Command实例的@command修饰符

#-*-coding:utf8-*-  
from flask_script import Manager  
from debug import app  
  
manager = Manager(app)  
 
@manager.command  
def hello():  
    'hello world'  
    print 'hello world'  
  
if __name__ == '__main__':  
    manager.run()  

结果:
python manager.py hello
> hello world

3.使用Command实例的@option修饰符

#-*-coding:utf8-*-  
from flask_script import Manager  
from debug import app  
  
manager = Manager(app)  
 
@manager.option('-n', '--name', dest='name', help='Your name', default='world')  
@manager.option('-u', '--url', dest='url', default='www.csdn.com')  
def hello(name, url):  
    'hello world or hello <setting name>'  
    print 'hello', name  
    print url  
  
if __name__ == '__main__':  
    manager.run()  

结果:
python manager.py hello
>hello world
>www.csdn.com

python manager.py hello -n sissiy -u www.sissiy.com
> hello sissiy
>www.sissiy.com

参考文档

 类似资料: