flask-script

栏目: Python · 发布时间: 6年前

内容简介:flask-script

关于flask_script

flask_script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任务;使得脚本和系统分开;

Flask Script和Flask本身的工作方式类似,只需定义和添加从命令行中被Manager实例调用的命令;

官方文档: http://flask-script.readthedocs.io/en/latest/

要使用flask-script需要先安装

pip install flask_script

创建并运行命令

首先,创建一个 Python 模板运行命令脚本,可起名为manager.py;

在该文件中,必须有一个Manager实例,Manager类追踪所有在命令行中调用的命令和处理过程的调用运行情况;

Manager只有一个参数——Flask实例,也可以是一个函数或其他的返回Flask实例;

调用manager.run()启动Manager实例接收命令行中的命令;


#-*-coding:utf8-*-  
from flask_script import Manager  
from debug import app  
  
manager = Manager(app)  
  
if __name__ == '__main__':  
    manager.run()  

其次,创建并加入命令;

有三种方法创建命令,即创建Command子类、使用@command修饰符、使用@option修饰符;

第一种——创建Command子类

Command子类必须定义一个run方法;

举例:创建Hello命令,并将Hello命令加入Manager实例;


from flask_script import Manager  ,Server
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())  
# 自定义命令二:

manager.add_command("runserver", Server()) #命令是runserver
if __name__ == '__main__':  
    manager.run()  

执行如下命令:

python manager.py hello

> hello world

python manager.py runserver

> hello world

第二种——使用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()  
 

该方法创建命令的运行方式和Command类创建的运行方式相同;

python manager.py hello

> hello world

第三种——使用Command实例的@option修饰符

复杂情况下,建议使用@option;

可以有多个@option选项参数;


from flask_script import Manager  
from debug import app  
  
manager = Manager(app)  
 
@manager.option('-n', '--name', dest='name', help='Your name', default='world')    #命令既可以用-n,也可以用--name,dest="name"用户输入的命令的名字作为参数传给了函数中的name
@manager.option('-u', '--url', dest='url', default='www.csdn.com')  #命令既可以用-u,也可以用--url,dest="url"用户输入的命令的url作为参数传给了函数中的url

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 manager.py hello -name sissiy -url www.sissiy.com

> hello sissiy

>www.sissiy.com


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java编程思想 (第4版)

Java编程思想 (第4版)

[美] Bruce Eckel / 陈昊鹏 / 机械工业出版社 / 2007-6 / 108.00元

本书赢得了全球程序员的广泛赞誉,即使是最晦涩的概念,在Bruce Eckel的文字亲和力和小而直接的编程示例面前也会化解于无形。从Java的基础语法到最高级特性(深入的面向对象概念、多线程、自动项目构建、单元测试和调试等),本书都能逐步指导你轻松掌握。 从本书获得的各项大奖以及来自世界各地的读者评论中,不难看出这是一本经典之作。本书的作者拥有多年教学经验,对C、C++以及Java语言都有独到......一起来看看 《Java编程思想 (第4版)》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具