当前位置:网站首页>Flask generates swagger documents
Flask generates swagger documents
2022-07-03 18:50:00 【Hua Weiyun】
flask Automatic generation swagger Of api Interface document
- install flask-restplus Third party package , Use pip install flask-restplus Can be installed .
- In an ordinary normal flask Under application project structure , It should be extensions.py Write code under , Because this is the code writing place for program extension . Guide pack , Import flask_restplus Under the Api,Resource,fields. Get one app example . And carry on namespace Writing . The code is as follows :
api = Api(doc='/swagger') api.init_app(app, version='1.0', title='Data Visualization And Analysis API', description='A Charting and Data analysis API') bar_line = api.namespace('drawing bar and line', path='/', description="draw bar and line chart") pie = api.namespace('drawing pie', path='/', description="draw pie chart") radar = api.namespace('drawing radar', path='/', description="draw radar chart") scatter = api.namespace('drawing scatter', path='/', description="draw scatter chart") data_analysis = api.namespace('data analysis', path='/', description="data analysis") Get an instance Api object ,app Is an instantiated flask object , By instantiating Api When the object passes doc Parameters can specify the route through which the final interface document can be accessed .api.namespace : It's a namespace , Many interfaces have get,post, The namespace separates them , It can be understood as a blueprint .
path: Represents their routing address , Here they all use route The address of , If you don't write it, you will change the namespace name Add to the front of the routing address
description: It is a general comment on all interfaces under this group .
- adopt api.model To describe the requested request and Responsive response, adopt api.namespace.parser To describe the requested headers Parameters .
The code example is as follows :
# Use parser To describe the interface headers and query bar_line_parameter = bar_line.parser() bar_line_parameter.add_argument('Authorization', location='headers', default="a") bar_line_parameter.add_argument('User-Agent', location='headers', default="ua") # Use model To describe the request body of the interface bar_line_model = api.model('Bar_Line_Request', { "type": fields.String(default="bar"), "title": DictItem(required=True, default={}, description="chart title option"), "item": DictItem(required=True, default={}, description="chart series item option"), "xaxis": DictItem(required=True, default={}, description="chart xaxis option"), "yaxis": DictItem(required=True, default={}, description="chart yaxis option"), "grid": DictItem(required=True, default={}, description="chart grid option"), "legend": DictItem(required=True, default={}, description="chart legend option"), "tooltip": DictItem(required=True, default={}, description="chart tooltip option"), "background": DictItem(required=True, default={}, description="chart tooltip option"), }, description="request api needed body parameter") # Use model To describe the response of the interface bar_line_response = api.model('bar_line_Response', { 'data': DictItem(required=True, default=bar_line_response_data_default, description="chart option"), 'status': fields.Integer(required=True, default=200, description="response status"), 'msg': fields.String(required=True, default="successful", description="api response message") }) Above , among bar_line yes api.namespace() The return object of , Use parser Of add_argument() Method to add headers , or query Request required parameters in , You can also define default values .
Use model To describe the body of the request , The response is also .model You need to specify a unique key value , And a {} Dictionary key value pairs , In the dictionary key value pair key The value is the value to be transmitted name,value It's through flask-restplus Under the fields To specify the data type and default value description Value .
If fields The data type provided in cannot meet the requirements of , You can inherit through custom classes fields.Row , And realize format Method , To use custom data types . In code DictItem Custom data types .
- The above defined model,parser Apply to the interface . By means of ornaments , The code is as follows .
# Use api.namespace.route To specify the access route of the interface , Use description To describe the interface @bar_line.route('/api/chart/draw/bar_and_line', doc={"description": " Returns the of the chart echarts Configuration item information , When the request parameter configuration is empty, the chart of the default configuration is returned, which is an example , Otherwise, the corresponding complete chart configuration information is returned according to the requested configuration parameters "}) # there api.namespace.expect Need to work with the above api.namespace.expect In combination with @bar_line.expect(bar_line_parameter) # Need to inherit from Resource class class BarLineOption(Resource): # doc Used to describe interfaces ,body=X Specify the requested body describe @bar_line.doc('Return to bar and line chart configuration item') @bar_line.doc(body=bar_line_model) # marshal_with Specifies the description of the response @bar_line.marshal_with(bar_line_response) # What methods does the interface support , Just define a method . def post(self): return {'data': {}, "status": 200, "msg": "successful"}边栏推荐
- 2022-2028 global petroleum pipe joint industry research and trend analysis report
- HOW TO WRITE A DAILY LAB NOTE?
- 为什么要做特征的归一化/标准化?
- 198. Looting - Dynamic Planning
- Boost. Asio Library
- Shell script return value with which output
- SQL custom collation
- How to design a high concurrency system
- Typescript official website tutorial
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
猜你喜欢

2022-2028 global plasmid DNA cdmo industry research and trend analysis report

SQL custom collation

leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】

组策略中开机脚本与登录脚本所使用的用户身份

2022-2028 global solid phase extraction column industry research and trend analysis report

Transformer T5 model read slowly
![[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult

FBI warning: some people use AI to disguise themselves as others for remote interview

22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
![235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]](/img/f5/f2d244e7f19e9ddeebf070a1d06dce.png)
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
随机推荐
Boost.Asio Library
2022-2028 global scar care product industry research and trend analysis report
How to quickly view the inheritance methods of existing models in torchvision?
变化是永恒的主题
042. (2.11) do it when it's time to do it
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
Integrated easy to pay secondary domain name distribution system
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Sustainable service business models
Understanding of database architecture
Software development freelancer's Road
[Godot] add menu button
4. Load balancing and dynamic static separation
Dart JSON编码器和解码器剖析
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
简述服务量化分析体系
为什么要做特征的归一化/标准化?
2022-2028 global plasmid DNA cdmo industry research and trend analysis report
“google is not defined” when using Google Maps V3 in Firefox remotely