Flask之flask-script模塊使用
Flask Script擴(kuò)展提供向Flask插入外部腳本的功能,包括運(yùn)行一個(gè)開發(fā)用的服務(wù)器,一個(gè)定制的Python shell,設(shè)置數(shù)據(jù)庫(kù)的腳本,cronjobs,及其他運(yùn)行在web應(yīng)用之外的命令行任務(wù);使得腳本和系統(tǒng)分開;
Flask Script和Flask本身的工作方式類似,只需定義和添加從命令行中被Manager實(shí)例調(diào)用的命令;
官方文檔:http://flask-script.readthedocs.io/en/latest/
創(chuàng)建并運(yùn)行命令
首先,創(chuàng)建一個(gè)Python模板運(yùn)行命令腳本,可起名為manager.py;
在該文件中,必須有一個(gè)Manager實(shí)例,Manager類追蹤所有在命令行中調(diào)用的命令和處理過程的調(diào)用運(yùn)行情況;
Manager只有一個(gè)參數(shù)——Flask實(shí)例,也可以是一個(gè)函數(shù)或其他的返回Flask實(shí)例;
調(diào)用manager.run()啟動(dòng)Manager實(shí)例接收命令行中的命令;
#-*-coding:utf8-*- from flask_script import Manager from debug import app manager = Manager(app) if __name__ == '__main__': manager.run()
其次,創(chuàng)建并加入命令;
有三種方法創(chuàng)建命令,即創(chuàng)建Command子類、使用@command修飾符、使用@option修飾符;
第一種——?jiǎng)?chuàng)建Command子類
Command子類必須定義一個(gè)run方法;
舉例:創(chuàng)建Hello命令,并將Hello命令加入Manager實(shí)例;
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()
執(zhí)行如下命令:
python manager.py hello
> hello worldpython manager.py runserver
> hello world
第二種——使用Command實(shí)例的@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()
該方法創(chuàng)建命令的運(yùn)行方式和Command類創(chuàng)建的運(yùn)行方式相同;
python manager.py hello
> hello world
第三種——使用Command實(shí)例的@option修飾符
復(fù)雜情況下,建議使用@option;
可以有多個(gè)@option選項(xiàng)參數(shù);
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"用戶輸入的命令的名字作為參數(shù)傳給了函數(shù)中的name
@manager.option('-u', '--url', dest='url', default='www.csdn.com') #命令既可以用-u,也可以用--url,dest="url"用戶輸入的命令的url作為參數(shù)傳給了函數(shù)中的url
def hello(name, url):
'hello world or hello <setting name>'
print 'hello', name
print url
if __name__ == '__main__':
manager.run()
運(yùn)行方式如下:
python manager.py hello
>hello world
>www.csdn.compython manager.py hello -n sissiy -u www.sissiy.com
> hello sissiy
>www.sissiy.compython manager.py hello -name sissiy -url www.sissiy.com
> hello sissiy
>www.sissiy.com
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用
這篇文章主要介紹了Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python 解決相對(duì)路徑問題:"No such file or directory"
這篇文章主要介紹了Python 解決相對(duì)路徑問題:"No such file or directory"具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-06-06
Python xlwt設(shè)置excel單元格字體及格式
這篇文章主要為大家詳細(xì)介紹了Python xlwt設(shè)置excel單元格字體及格式的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
python數(shù)字轉(zhuǎn)對(duì)應(yīng)中文的方法總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于python數(shù)字轉(zhuǎn)對(duì)應(yīng)中文的方法總結(jié)內(nèi)容,有興趣的朋友們可以跟著猜嘗試測(cè)試下。2021-08-08
在Python3 numpy中mean和average的區(qū)別詳解
今天小編就為大家分享一篇在Python3 numpy中mean和average的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-08-08
python中requests爬去網(wǎng)頁(yè)內(nèi)容出現(xiàn)亂碼問題解決方法介紹
這篇文章主要介紹了python中requests爬去網(wǎng)頁(yè)內(nèi)容出現(xiàn)亂碼問題解決方法,2017-10-10

