zoukankan      html  css  js  c++  java
  • 【FLASK】Flask-Script

    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
    

    参考文档

    不怕世界大,就怕格局小
  • 相关阅读:
    Python基础之zip和enumerate
    python3中map()函数用法
    python列表推导式
    python面试常问的几个内置装饰器:@staticmethod、@classmethod和@property
    linux的解压与压缩
    python中 s f各种转移字符含义
    fixture 调用函数名传参(转载)
    3.css选择器
    实战有感3
    实战有感2-轮播图
  • 原文地址:https://www.cnblogs.com/wangjian1226/p/10511674.html
Copyright © 2011-2022 走看看