Flask学习【第9篇】:Flask-script组件
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
Flask学习【第9篇】:Flask-script组件的更多相关文章
- Angular2+学习第3篇 基本知识-组件
一.插值表达式 基本用法与ng1一样. 可以使用 Angular 内置的 json 管道,来显示对象信息,管道用来格式化数据 import { Component } from '@angular/c ...
- Flask学习【第10篇】:自定义Form组件
wtforms源码流程 实例化流程分析 1 # 源码流程 2 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: meta类读取到cls._ ...
- Flask 学习篇二:学习Flask过程中的记录
Flask学习笔记: GitHub上面的Flask实践项目 https://github.com/SilentCC/FlaskWeb 1.Application and Request Context ...
- Flask学习【第11篇】:整合Flask中的一些知识点
SQLAlchemy-Utils 由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 import datetime from sq ...
- Flask 学习篇一: 搭建Python虚拟环境,安装flask,并设计RESTful API。
前些日子,老师给我看了这本书,于是便开始了Flask的学习 GitHub上的大神,于是我也在GitHub上建了一个Flask的项目. 有兴趣可以看看: https://github.com/Silen ...
- Flask【第10篇】:自定义Form组件
自定义Form组件 一.wtforms源码流程 1.实例化流程分析 1 # 源码流程 2 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: ...
- Flask学习【第6篇】:Flask中的信号
实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settings ...
- Flask学习【第3篇】:蓝图、基于DBUtils实现数据库连接池、上下文管理等
小知识 子类继承父类的三种方式 class Dog(Animal): #子类 派生类 def __init__(self,name,breed, life_value,aggr): # Animal. ...
- Flask学习【第2篇】:Flask基础
知识点回顾 flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 实例化Flask对象,里面是有参数的 app = Flask(__name__,template_ ...
- [ZHUAN]Flask学习记录之Flask-SQLAlchemy
From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...
随机推荐
- JavaScript-switch-case运用-案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 六 js函数和this
js的所有代码都是由funtion组成,funtion即函数的类型. 一.函数有两种写法 -----1.定义式 function test() { //定义一个函数 console.log(" ...
- python-demo实例
1.turtle库与蟒蛇案例 import turtle def drawSnake(rad,angle,len,neckrad): for i in range(len): turtle.circl ...
- wxpython(python3.5)安装
安装步骤: http://blog.csdn.net/xiaodong193/article/details/51920283 注意:安装软件前需要阅读其中的README.txt,可快速知道安装方法, ...
- eclipse xml 文件添加注解快捷键
eclipse xml 文件注解快捷键: <!-- --> Ctrl + shift + / 添加注解 Ctrl + shift + \ 取消注解
- Knowing is not enough; we must apply. Willing is not enough; we must do.
Knowing is not enough; we must apply. Willing is not enough; we must do. 仅限于知道是不够的,我们必须去实践:单纯的希望是不够的 ...
- Java综合高级篇
1.你用过哪些集合类? 大公司最喜欢问的Java集合类面试题 40个Java集合面试问题和答案 java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法. ja ...
- linux常用命令:mv 命令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...
- eos中BM与有BM特色的去中心化。区块链世界,白皮书为共识,代码为法律。
比特币挖矿是谁算力高,谁更容易挖到新的比特币,而BM认为这太浪费资源了,于是设计了DPoS:在DPoS系统里,大家不再挖矿.而是指定几个人负责记账,不叫矿工,而叫见证人.比特股里开始是101人,EOS ...
- JustOj 1910: 人见人爱A+B
[提交][状态][讨论版] 题目描述 北大的acm上面已经有10来道A+B的题目了,相信这些题目曾经是大家的最爱,希望今天的这个A+B能给大家带来好运,也希望这个题目能唤起大家对ACM曾经的热爱. ...