Flask之flask-script模块使用

作者:不_一 时间:2022-02-18 11:01:13 

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

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

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

创建并运行命令

首先,创建一个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

来源:https://www.cnblogs.com/buyisan/p/8270283.html

标签:Flask,flask-script
0
投稿

猜你喜欢

  • Python操作MongoDB数据库的方法示例

    2022-09-03 13:48:40
  • MySQL查询优化

    2009-03-09 14:41:00
  • php函数serialize()与unserialize()用法实例

    2023-11-19 00:53:23
  • 利用pyshp包给shapefile文件添加字段的实例

    2023-08-12 15:48:38
  • python中sys模块是做什么用的

    2021-04-30 10:04:48
  • python openpyxl使用方法详解

    2021-12-23 14:49:18
  • Oracle数据库中通用的函数实例详解

    2023-07-08 04:49:00
  • python基于twisted框架编写简单聊天室

    2021-12-12 17:56:51
  • pyqt5 QProgressBar清空进度条的实例

    2022-11-28 03:04:08
  • sql server vs10安装之后一些列问题

    2012-01-05 19:22:41
  • JS与CSS合并写在同一份文件

    2010-01-23 12:33:00
  • 基于display:table的CSS布局

    2008-10-30 10:38:00
  • PHP基于phpqrcode类库生成二维码过程解析

    2023-11-17 19:06:35
  • python中的实例方法、静态方法、类方法、类变量和实例变量浅析

    2021-11-06 01:52:14
  • Python实现的特征提取操作示例

    2023-02-07 06:08:04
  • Python教程之Python多态的深层次理解

    2021-07-30 07:50:53
  • asp连接MYSQL数据库的连接字符串(参数OPTION)

    2009-03-09 18:24:00
  • 如何在服务器端调用winzip命令行对文件压缩和解压

    2008-01-26 20:44:00
  • 理解SQL SERVER中的逻辑读,预读和物理读

    2012-01-05 19:32:29
  • Python实现数通设备端口使用情况监控实例

    2021-02-09 23:56:41
  • asp之家 网络编程 m.aspxhome.com