Flask - 第三方插件简单总结

目录

一、flasgger.Swagger

1-1 相关文档链接整理

二、flask_cors - CORS跨域请求处理

三、loguru - 日志文件的自动创建和写入

3-1 record dict

四、flask_sqlalchemy

4-1 内部ORM支持类型查询

4-2 使用错误总结

4-2-1 ModuleNotFoundError:No module named ‘MySQLdb’

4-2-2 sqlalchemy.exc.InternalError 

4-2-3 PermissionError: [Errno 13] Permission denied

五、flask_restful

5-1 reqparse - 允许在单个请求的上下文中添加和解析多个参数


一、flasgger.Swagger

Restful接口的文档在线自动生成+功能测试功能软件,用于API接口文档的查看和测试 (Django内的渲染器概念)

http://127.0.0.1:5000/apidocs/

使用上述链接查询api文档(固定路径)

1-1 相关文档链接整理

Flasgger使用心得 - 简单使用

swagger 官方网页 - 需科学上网

swagger使用总结 - 外文

二、flask_cors - CORS跨域请求处理

官方文档

三、loguru - 日志文件的自动创建和写入

每当输出info、debug等对应信息,则自动写入指定文件、执行函数等

API文档

loguru - github

3-1 record dict

记录只是一个Python字典,可以通过message.record从接收器访问。它包含日志记录调用的所有上下文信息(时间,函数,文件,行,级别等)。它的每个键都可以以处理程序的格式使用,因此相应的值可以正确显示在记录的消息中(例如“{level}” - >“INFO”)。某些记录的值是具有两个或多个属性的对象,可以使用“{key.attr}”格式化(“{key}”默认显示一个)。格式化指令如“{key:> 3}”也有效,对时间特别有用(见下文)。

The record is just a Python dict, accessible from sinks by message.record. It contains all contextual information of the logging call (time, function, file, line, level, etc.). Each of its key can be used in the handler’s format so the corresponding value is properly displayed in the logged message (eg. "{level}" -> "INFO"). Some record’s values are objects with two or more attributes, those can be formatted with "{key.attr}" ("{key}" would display one by default). Formatting directives like "{key: >3}" also works and is particularly useful for time (see below).

Key Description Attributes
elapsed The time elapsed since the start of the program See datetime.timedelta
exception The formatted exception if any, None otherwise type, value, traceback
extra The dict of attributes bound by the user (see bind()) None
file The file where the logging call was made name (default), path
function The function from which the logging call was made None
level The severity used to log the the message name (default), no, icon
line The line number in the source code None
message The logged message (not yet formatted) None
module The module where the logging call was made None
name The __name__ where the logging call was made None
process The process in which the logging call was made name, id (default)
thread The thread in which the logging call was made name, id (default)
time The aware local time when the logging call was made See datetime.datetime

 

四、flask_sqlalchemy

flask-sqlalchemy - 官方文档

4-1 内部ORM支持类型查询

4-2 使用错误总结

4-2-1 ModuleNotFoundError:No module named ‘MySQLdb’

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:Apassword@localhost/mysqlalchemy'
# 在连接url内添加指定驱动,+pymysql

4-2-2 sqlalchemy.exc.InternalError 

数据库名与本地不匹配

4-2-3 PermissionError: [Errno 13] Permission denied

!!!未解决!!!

五、flask_restful

官方文档 - 全英

5-1 reqparse - 允许在单个请求的上下文中添加和解析多个参数

(1)尽管 Flask 能够简单地访问请求数据(比如查询字符串或者 POST 表单编码的数据),验证表单数据仍然很痛苦。Flask-RESTful 内置了支持验证请求数据,它使用了一个类似 argparse 的库。

(2)需要注意地是与 argparse 模块不同,reqparse.RequestParser.parse_args() 返回一个 Python 字典而不是一个自定义的数据结构。

from flask_restful import reqparse

parser = reqparse.RequestParser()
parser.add_argument('foo')
parser.add_argument('int_bar', type=int)
args = parser.parse_args()
from flask_restful import reqparse
from flask import Flask
from flask_restful import Resource, Api


app = Flask(__name__)
api = Api(app)


class HelloWorld(Resource):
    def get(self):
        # reqparse 在request请求的body体内查询指定参数,并返回一个python字典
        parser = reqparse.RequestParser()
        parser.add_argument('rate', type=int, help='Rate cannot be converted')
        parser.add_argument('name')
        parser.add_argument('hello')
        args = parser.parse_args()
        print(args)

        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')


if __name__ == '__main__':
    app.run(debug=True)


# {'rate': None, 'name': None, 'hello': '123'}

猜你喜欢

转载自blog.csdn.net/qq_33961117/article/details/88793089