Flask系列(九)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系列(九)flask-script组件的更多相关文章
- Flask系列(二)Flask基础
知识点回顾 1.flask依赖wsgi,实现wsgi的模块:wsgiref(django),werkzeug(flask),uwsgi(上线) 2.实例化Flask对象,里面是有参数的 app = F ...
- Flask系列(六)Flask实例化补充及信号
一.实例化补充 instance_path和instance_relative_config是配合来用的. 这两个参数是用来找配置文件的,当用app.config.from_pyfile('setti ...
- flask系列九之使用falsk建立项目总结
待续.... 源码地址:https://gitee.com/FelixBinCloud/ZhiLiaoDemo/tree/master/ZhiLiao
- Flask系列(五)Flask实现分页
一.flask分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object): ""& ...
- React Native 系列(九) -- Tab标签组件
前言 本系列是基于React Native版本号0.44.3写的.很多的App都使用了Tab标签组件,例如QQ,微信等等,就是切换不同的选项,显示不同的内容.那么这篇文章将介绍RN中的Tab标签组件. ...
- Flask系列06--(中间件)Flask的特殊装饰器 before_request,after_request, errorhandler
一.使用 Flask中的特殊装饰器(中间件)方法常用的有三个 @app.before_request # 在请求进入视图函数之前 @app.after_request # 在请求结束视图函数之后 响应 ...
- Flask系列(四)Flask实现简单页面登陆
from flask import Flask,render_template,request,redirect,session app = Flask(__name__,template_folde ...
- vue 开发系列(九) VUE 动态组件的应用
业务场景 我们在开发表单的过程中会遇到这样的问题,我们选择一个控件进行配置,控件有很多中类型,比如文本框,下来框等,这些配置都不同,因此需要不同的配置组件来实现. 较常规的方法是使用v-if 来实现, ...
- Flask 系列之 部署发布
说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过 Windows 的 WSL,将我们的项目网站部署到 ...
- 【Python】Flask系列-模板笔记
Jinja2模板 Jinja2模板传参 如何渲染模板: 模板放在templates文件夹下 从flask中导入render_template函数. 在视图函数中,使用render_template函数 ...
随机推荐
- 记录下自己常用的全框架HTML代码
纯粹记录下,没有任何意义. 也不推荐使用 <frameset rows="> <frame src=" name="topFrame" scr ...
- schema in oracle
the conception of schema is different in different db software. here i just refer to oracle schema. ...
- osgEarth的agglite插件使用例子feature_rasterize.earth
<!-- osgEarth Sample Demonstrates use of the "agglite" feature rasterization driver. -- ...
- LeetCode——Search Insert Position
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
- su命令cannot set groups: Operation not permitted的解决方法
版权声明:本文由曾倩倩原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/103 来源:腾云阁 https://www.qclo ...
- 从TCP三次握手说起--浅析TCP协议中的疑难杂症(1)
版权声明:本文由黄日成原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/73 来源:腾云阁 https://www.qclou ...
- jquery类似方法的比较(一)
1. $(div + p) & $(div ~ p) & $(div p) & $(div > p) $(div + p)选择紧挨在div后面的P元素 $(div ~ p ...
- 从一次渗透谈到linux如何反弹shell
零.绪论 背景: ThinkPHP框架的--> 找到一个OS命令注入(很简单的Burp可以直接扫出来的那种):页面配置系统默认网关处. 一.渗透过程 1.首先看了一下,没有回显. 2.用ceye ...
- html to pdf小工具,支持evernote导出的html和firefox插件scrapbook
周末花了一天时间用wpf写了一个html转换为pdf的小工具. 已经在win7 32位 和win8 64两台机器上测试,目前基本可用,特拿来分享. 程序下载地址:http://pan.baidu.co ...
- [SQL] 常用查询脚本
查询哪些存储过程使用了某个表 select b.name from syscomments a,sysobjects b where a.id=b.id and a.text LIKE '%ftblo ...