当前位置:网站首页>浅谈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
边栏推荐
- Redis(八)集群
- SiamFC:用于目标跟踪的全卷积孪生网络 fully-convolutional siamese networks for object tracking
- (error) NOAUTH Authentication required.
- Jmeter连接Mysql和Mysql编码问题
- Decimal工具类
- pytorch指定GPU
- 3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
- db2数据库备份恢复问题
- C language: adjust the order of odd and even numbers
- 配置本地yum源仓库
猜你喜欢
随机推荐
主键id,Snowflake雪花算法,优点:生成有顺序的id,提高数据库的性能
【机器学习】支持向量机(SVM)代码练习
ssh:connect to host master port 22:Network is unreachable
原生JDBC操作数据库
工信部等四部门推动绿色智能家居产品下乡
(四)BP神经网络预测(上)
SSM integration development case
测试和开发之间的恩恩怨怨
9.进程和计划任务管理(1)
NAT地址转换的原理与配置
VOC格式标签转YOLO格式
低成本、大容量、高交互…Polkadot 引领 GameFi 实现新突破
String类创建的对象在JVM中的内存分配和equals与==的区别
Four departments including the Ministry of Industry and Information Technology promote green smart home products to the countryside
LeetCode·每日一题·636.函数的独占时间·栈模拟
LVM与磁盘配额
(二)、灰色预测模型(GM1,1)
db2数据库备份恢复问题
转换为onnx模型错误汇总
IDEA文件UTF-8格式控制台输出中文乱码