当前位置:网站首页>浅谈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
边栏推荐
猜你喜欢
Selenium测试案例一步步学之(2)Selenium自动测试脚本模块化(下)
Jmeter连接Mysql和Mysql编码问题
Solidworks 2022 Inspection新增功能:光学字符识别、可自定义的检查报告
SiamFC:用于目标跟踪的全卷积孪生网络 fully-convolutional siamese networks for object tracking
如何把无用的代码注释为 Deprecated 弃用
[STL]vector
HOOPS助力 SolidWorks edrawings 引入AR/VR技术
MYSQLWorkbench看数据库ER图
ncnn 推理猫狗识别
C语言:打印菱形
随机推荐
Result consisted of more than one row
CoCube传感器MPU6050笔记
测试流程
安全的Md5加密:两次加密(加盐)
SOLIDWORKS Simulation教程:计算物体的固有频率
“互联网+”大学生创新创业大赛经历
(三)、时间序列预测
3D精彩案例,清软英泰建成综合轻量化显示平台!
Oracle 限制时将空值排除
Shell--常用小工具(sort、uniq、tr、cut)
3安装及管理程序
.net(三) 项目结构
C language: reverse character order
.net(二) 配置数据库
Forest Program DFS + tanjar cactus
RestFul,会话技术,Fiddler
.net(五) 业务层实现
LeetCode·每日一题·761.特殊的二进制序列·分治
(五)、马尔科夫预测模型
BGP路由协议的那些事?(中)