flask第九篇flask-script组件

呆萌小河马的博客 呆萌小河马的博客     2022-10-16     700

关键词:

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

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

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

1 创建并运行命令

首先,创建一个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第九篇——url_for(代码片段)

上一节说的是没有参数的url_for,如果没有参数,可以直接url_for(‘函数名‘)那如果我们构造的函数是:@app.route(‘/login/<page_id>/‘)deflogin(page_id):  returnu‘登录页面‘这时候url_for该怎么传呢?我们先来看一下url_for的源... 查看详情

flask-script组件

...加从命令行中被Manager实例调用的命令;官方文档:http://flask-script.readthedoc 查看详情

87flask之flask-script组件

...加从命令行中被Manager实例调用的命令;官方文档:http://flask-script.readthedoc 查看详情

flask系列flask-script组件(代码片段)

...加从命令行中被Manager实例调用的命令;官方文档:http://flask-script.readthedoc 查看详情

flask-script组件(代码片段)

...加从命令行中被Manager实例调用的命令;官方文档:http://flask-script.readthedoc 查看详情

第九篇flask的before_request和after_request(代码片段)

Flask我们已经学习很多基础知识了,现在有一个问题我们现在有一个Flask程序其中有3个路由和视图函数,如下:fromflaskimportFlaskapp=Flask(__name__)#type:Flask@app.route("/login")deflogin():return"Login"@app.route("/index")defindex():return"Index"@app.route("/home")def... 查看详情

业余草springcloud教程|第九篇:服务链路追踪(springcloudsleuth)(finchley版本)

这篇文章主要讲述服务追踪组件zipkin,SpringCloudSleuth集成了zipkin组件。一、简介AddsleuthtotheclasspathofaSpringBootapplication(seebelowforMavenandGradleexamples),andyouwillseethecorrelationdatabeingcollectedinlogs,aslongasyou 查看详情

第九篇:vue组件的生命周期钩子(代码片段)

组件的生命周期钩子一、组件的生命周期:一个组件从创建到销毁的整个过程二、生命周期钩子:在一个组件生命周期中,会有很多特殊的时间节点,且往往会在特定的时间节点完成一定的逻辑,特殊的事件节点可以绑定钩子注... 查看详情

第九篇springsecurity核心过滤器-securitycontextpersistencefilter

SpringSecurity核心过滤器-SecurityContextPersistenceFilter一、SpringSecurity中的核心组件  在SpringSecurity中的jar分为4个,作用分别为jar作用spring-security-coreSpringSecurity的核心jar包,认证和授权的核心代码都在这里面spring-security-co 查看详情

flask-migrate(代码片段)

...migrate吗?答案在这里该诉你,如果你同时拥有两个三方组件Flask-Script和Flask-Migrate那么就支持这样的操作 Flask-Script的安装与使用可参考之前的博客内容一、安装Flask-Migratepip3installFlask-Migrate二、将Flask-Migrate加入到Flask项目中PS:注... 查看详情

第九篇springsecurity核心过滤器-securitycontextpersistencefilter(代码片段)

SpringSecurity核心过滤器-SecurityContextPersistenceFilter一、SpringSecurity中的核心组件  在SpringSecurity中的jar分为4个,作用分别为jar作用spring-security-coreSpringSecurity的核心jar包,认证和授权的核心代码都在这里面spring-security-co 查看详情

第九篇googlenet——论文翻译

文章目录摘要 查看详情

第九篇道

第九篇 道  “道”这个字出现在人们的视野中,已经有很悠久的历史。但是人们对于“道”这个字的理解非常模糊,都是盲目地去猜测,不知道这个字真正的意义是什么。其实,“道”就是轨道,也可以称之为“宇宙... 查看详情

如何使用 Flask-Script 和 Gunicorn

】如何使用Flask-Script和Gunicorn【英文标题】:HowtouseFlask-ScriptandGunicorn【发布时间】:2013-01-1201:25:15【问题描述】:我正在使用Flask的内置开发服务器开发Flask应用程序。我使用Flask-Script启动它。我想切换到使用Gunicorn作为Web服务器... 查看详情

flask-script

flask-script的功能: 1.增加了一个runserver的命令,可以自定制域名和端口 pythonmanage.pyrunserver-h127.0.0.1-p8001 2.可以自定义命令,是位置传参数。@manager.commanddefcreate_table(arg):‘‘‘自定义的命令:paramarg::return:‘‘‘print(arg)调用的命... 查看详情

flask-migrate(代码片段)

...migrate吗?答案在这里该诉你,如果你同时拥有两个三方组件Flask-Script和Flask-Migrate那么就支持这样的动作首先你要有几个准备工作第十五章的知识回顾第十五章的项目下载废话不多说,直接进入正题1.安装Flask-MigratepipinstallFlask-Migrate2.... 查看详情

flask:flask-script;多app应用;wtforms;sqlchmy

一. flask-script一.flask-script简介1.什么是flask-script:是一个让你的命令行支持自定义命令的工具,它为Flask程序添加一个命令行解释器。可以让我们的程序从命令行直接执行相应的程序(用于实现类似于django中python3manage.pyrunserver...... 查看详情

第九篇:进程与线程

一、paramiko模块二、ssh登录过程和上传下载三、进程和线程简绍四、多线程、多线程实例五、守护线程六、线程锁七、递归锁八、信号量九、线程间通信event十、queue消息队列十一、进程Queue数据传递十二、pipe管道通信十三、进程... 查看详情