当前位置:网站首页>浅谈Flask_script
浅谈Flask_script
2022-08-09 07:57:00 【scwMason】
参考文章https://flask-script.readthedocs.io/en/latest/
概念
Flask_script是一种Flask的交互式命令行工具。方便交互式调试、执行脚本。也就是方便以命令行的方式直接在终端中调试每个文件中的某个函数,一般有一下三种方法
方法一 创建Command子类
from flask_script import Manager,Server,Command
from flask import Flask
app=Flask(__name__)
manager=Manager(app)
class Hello(Command):
'hello world'
def run(sellf):
print("hello world")
manager.add_command('hello',Hello())
manager.add_command("runserver",Server())
if __name__=='__main__':
manager.run()>>python demo1.py hello
>>hello world python demo1.py runserver
* Serving Flask app "demo1" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)runserver就是相当于app.run()
方法二 使用Command实例的@command符号
from flask_script import Manager,Server,Command
from flask import Flask
app=Flask(__name__)
manager=Manager(app)
@manager.command
def hello():
'hello world'
print('hello world')
if __name__=='__main__':
manager.run()方法三 使用Command实例的@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
边栏推荐
猜你喜欢
随机推荐
可能导致Loadrunner检查点中savecount为0的分析
The String class objects created by the JVM memory allocation and the difference between equals and = =
db2数据库备份恢复问题
Set集合
Anaconda use proxy
C: print the diamond
如何生成dll文件 采用VS2017生成dll文件(动态库文件)和lib文件(静态库文件)以C语言为例
(error) NOAUTH Authentication required.
转换为onnx模型错误汇总
Decimal工具类
Shell之函数与数组
Shell--常用小工具(sort、uniq、tr、cut)
Forest Program DFS + tanjar cactus
.net(三) 项目结构
Jmeter连接Mysql和Mysql编码问题
74HC595 chip pin description
定时任务组件Quartz
权限(下)
在今天这个特殊的日子,我想要开始我的代码技术博客之路
CUDA和cuDNN 安装10.0版本









