Flask-Script模块使用

Flask-Script的作用是可以通过命令行的形式来操作Flask。通过下面的例子,来简单了解一下吧

 

demo

#manage.py
from flask_script import Command,Manager
from app import app

#目的:练习 Flask-Script模块的使用
#安装依赖:pip install flask-script

manager = Manager(app)

# 1 使用@command 装饰器
# @manager.command
# def demo1():
#     print("hello demo1")

# 2 使用Command 类继承
class demo2(Command):

    def run(self):
        print("hello demo2")
manager.add_command('demo2', demo2())   #添加这个命令

# 3 使用option装饰器
@manager.option('-n','--name',dest='name')
def demo3(name):
    print("hello {}".format(name))

# 使用option装饰器适合添加多个参数
@manager.option('-n', '--name', dest='name', default='joe')
@manager.option('-u', '--url', dest='url', default=None)
def hello(name, url):
  if url is None:
      print("hello {}".format(name))
  else:
      print("hello {} from {}".format(name,url))

#如果这个文件以主脚本运行,就执行
if __name__ == "__main__":
    manager.run()

运行脚本 

(venv) E:\Flask-Web\MyProjetct\flask-script_test>python manage.py demo2
hello demo2

(venv) E:\Flask-Web\MyProjetct\flask-script_test>python manage.py hello -u www.benmoom.club -n wangjian
hello wangjian from www.benmoom.club

 

全部评论

相关推荐

八股刚起步,看了javaguide,小林coding,还有面渣,感觉面渣是体验最好的,请问只看面渣够用吗,有不完善的需要补吗?
码农索隆:先背最基础的知识,然后理解情景题,现在面试大多数喜欢问情景题,更考验面试者的基础和临场发挥情况
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务