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组件的更多相关文章

  1. Angular2+学习第3篇 基本知识-组件

    一.插值表达式 基本用法与ng1一样. 可以使用 Angular 内置的 json 管道,来显示对象信息,管道用来格式化数据 import { Component } from '@angular/c ...

  2. Flask学习【第10篇】:自定义Form组件

    wtforms源码流程 实例化流程分析 1 # 源码流程 2 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: meta类读取到cls._ ...

  3. Flask 学习篇二:学习Flask过程中的记录

    Flask学习笔记: GitHub上面的Flask实践项目 https://github.com/SilentCC/FlaskWeb 1.Application and Request Context ...

  4. Flask学习【第11篇】:整合Flask中的一些知识点

    SQLAlchemy-Utils 由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 import datetime from sq ...

  5. Flask 学习篇一: 搭建Python虚拟环境,安装flask,并设计RESTful API。

    前些日子,老师给我看了这本书,于是便开始了Flask的学习 GitHub上的大神,于是我也在GitHub上建了一个Flask的项目. 有兴趣可以看看: https://github.com/Silen ...

  6. Flask【第10篇】:自定义Form组件

    自定义Form组件 一.wtforms源码流程 1.实例化流程分析 1 # 源码流程 2 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: ...

  7. Flask学习【第6篇】:Flask中的信号

    实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settings ...

  8. Flask学习【第3篇】:蓝图、基于DBUtils实现数据库连接池、上下文管理等

    小知识 子类继承父类的三种方式 class Dog(Animal): #子类 派生类 def __init__(self,name,breed, life_value,aggr): # Animal. ...

  9. Flask学习【第2篇】:Flask基础

    知识点回顾 flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 实例化Flask对象,里面是有参数的 app = Flask(__name__,template_ ...

  10. [ZHUAN]Flask学习记录之Flask-SQLAlchemy

    From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...

随机推荐

  1. Vue项目中跨域的几种方式

    经常使用vue + webpack搭建项目,但在请求某些json数据时存在跨域问题,此时有几种修改方法 1. 修改后台header, 但如果只是请求外部数据,是没法修改后台配置的 header('Ac ...

  2. 并查集(disjoint)

    合并,查找的集合 第一,建立两个函数,一个用来查找(find),一个用来合并(merge): 如下的一个模板: 题目: 亲戚(Relations) 或许你并不知道,你的某个朋友是你的亲戚.他可能是你的 ...

  3. mac connect to host localhost port 22: Connection refused

    在Mac OS X 10.10.5学习hadoop的过程中,输入命令ssh localhost得到 ssh: connect to host localhost port : Connection r ...

  4. java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition的解决方案

    今天在GitHub上面看到一个有意思的项目,下载下来,使用tomcat部署失败,出现异常,网上说JDK版本太高,改低,还是失败. 由于本人有个习惯,更喜欢把项目直接放入tomcat webapps 里 ...

  5. U盘自动拷贝

    描述:启动该程序后,自动检测U盘是否存在,若存在,将U盘中所有的文件拷贝到电脑的指定目录下. 注:本篇博文仅支持技术讨论,不用于数据的盗取之类的黑科技. 本程序基于Win32开发,主要是利用Win32 ...

  6. html5-button元素

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  7. Python - 1. Built-in Atomic Data Types

    From:http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.html ...

  8. Java多线程-----匿名内部类创建线程

       1.继承Thread类创建线程 package com.practise.createthread; public class AnonymousThread { public static v ...

  9. 【impala学习之一】impala

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 CM5.4 一.ImpalaImpala是基于Hive的大数 ...

  10. php小数加减精度问题,比特币计算精度问题

    php小数加减精度问题,比特币计算精度问题 在php开发时,有小数加减的场景.结果发现不能够等于预想的值,bccomp比较二个高精确度数字.语法: int bccomp(string left ope ...