Flask-Docs
Influence me to write documents may be the reason for the separation of code and documentation , sometimes write the code will forget to fill in the documentation , and can not be viewed in a timely manner , the use of Flask-Docs can solve my problem , this plug-in can be generated according to the code comments on the documentation page , the code comments to change the document can be updated in a timely manner , and support for offline document downloads , support for online debugging .
Flask-Docs Flask Api Documentation Automation Plugin
characterization
- Automatic generation of documentation based on code comments
- Support for offline markdown document download
- Support for Flask-RESTful
- Support for Flask-RESTX
- be in favor of
- Support online debugging
link (on a website)/kwkwc/flas...
mounting
pip3 install Flask-Docs
utilization
from flask import Flask from flask_docs import ApiDoc app = Flask(__name__) ApiDoc( app, title="Sample App", version="1.0.0", description="A simple app API", )
configure
# Use CDN # ["API_DOC_CDN"] = True # Disable document pages # ["API_DOC_ENABLE"] = False # SHA256 encrypted authorization password, e.g. admin here # echo -n admin | shasum -a 256 # ["API_DOC_PASSWORD_SHA2"] = "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918" # Methods allowed to be displayed # ["API_DOC_METHODS_LIST"] = ["GET", "POST", "PUT", "DELETE", "PATCH"] # Customize url_prefix # ["API_DOC_URL_PREFIX"] = "/docs/api" # RESTful Api class names to exclude # ["API_DOC_RESTFUL_EXCLUDE"] = ["Todo"] # Name of the Api blueprint to be displayed # ["API_DOC_MEMBER"] = ["api", "platform"] # Submembers to Exclude Api Function Name # ["API_DOC_MEMBER_SUB_EXCLUDE"] = ["delete_data"]
How to Write a Markdown Format Document
@@@
Include markdown formatting with "@@@" at the end of comments
@@@
View Documentation Page
http://127.0.0.1/docs/api/
Api demo
@("/add_data", methods=["POST"]) def add_data(): """Add some data @@@ ### args | args | nullable | request type | type | remarks | |-------|----------|--------------|------|----------| | title | false | body | str | blog title | | name | false | body | str | person's name | ### request ```json {"title": "xxx", "name": "xxx"} ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return jsonify({"api": "add data"})
@("/delete_data", methods=["GET"]) def delete_data(): """Delete some data @@@ ### args | args | nullable | request type | type | remarks | |--------|----------|--------------|------|--------------| | id | true | query | str | blog id | | name | false | query | str | person's name | ### request ``` http://127.0.0.1:5000/api/delete_data?name=xxx ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return jsonify({"api": "delete data"})
@("/get_something", methods=["GET"]) def get_something(): """Get some data @@@ ### request example ```python import requests url="http://127.0.0.1:5000/platform/get_something" try: print((url).text) except: pass ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return jsonify({"platform": "get something"})
Full Code
from flask import Blueprint, Flask, jsonify from flask_docs import ApiDoc app = Flask(__name__) ["API_DOC_MEMBER"] = ["api", "platform"] ApiDoc( app, title="Sample App", version="1.0.0", description="A simple app API", ) api = Blueprint("api", __name__) platform = Blueprint("platform", __name__) @("/add_data", methods=["POST"]) def add_data(): """Add some data @@@ ### args | args | nullable | request type | type | remarks | |-------|----------|--------------|------|----------| | title | false | body | str | blog title | | name | false | body | str | person's name | ### request ```json {"title": "xxx", "name": "xxx"} ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return jsonify({"api": "add data"}) @("/delete_data", methods=["GET"]) def delete_data(): """Delete some data @@@ ### args | args | nullable | request type | type | remarks | |--------|----------|--------------|------|--------------| | id | true | query | str | blog id | | name | false | query | str | person's name | ### request ``` http://127.0.0.1:5000/api/delete_data?name=xxx ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return jsonify({"api": "delete data"}) @("/get_something", methods=["GET"]) def get_something(): """Get some data @@@ ### request example ```python import requests url="http://127.0.0.1:5000/platform/get_something" try: print((url).text) except: pass ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return jsonify({"platform": "get something"}) app.register_blueprint(api, url_prefix="/api") app.register_blueprint(platform, url_prefix="/platform") if __name__ == "__main__": (host="0.0.0.0", port=5000, debug=True)
Flask-RESTful Api demo
from flask import Flask from flask_restful import Api, Resource from flask_docs import ApiDoc app = Flask(__name__) restful_api = Api(app) ApiDoc( app, title="Sample App Restful", version="1.0.0", description="A simple app restful API", ) class Todo(Resource): """Manage todo""" def post(self): """Add todo @@@ ### description > Add todo ### args | args | nullable | request type | type | remarks | |-------|----------|--------------|------|----------| | name | false | body | str | todo name | | type | false | body | str | todo type | ### request ```json {"name": "xx", "type": "code"} ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return {"todo": "post todo"} def get(self): """Get todo @@@ ### description > Get todo ### args | args | nullable | request type | type | remarks | |-------|----------|--------------|------|----------| | name | false | query | str | todo name | | type | true | query | str | todo type | ### request ``` http://127.0.0.1:5000/todo?name=xxx&type=code ``` ### return ```json {"code": xxxx, "msg": "xxx", "data": null} ``` @@@ """ return {"todo": "get todo"} restful_api.add_resource(Todo, "/todo") if __name__ == "__main__": (host="0.0.0.0", port=5000, debug=True)
Api demo
Currently only url_rule with the same class name is supported.
from import MethodView class TodoList(MethodView): """Manage todolist""" def put(self): """Change the data """ return jsonify({"todos": "put todolist"}) def delete(self): """Delete the data """ return jsonify({"todos": "delete todolist"}) app.add_url_rule("/todolist/", view_func=TodoList.as_view("todolist"))
Decorator @ApiDoc.change_doc
reuse comments
from flask_docs import ApiDoc return_json_str = '{"code": xxxx, "msg": "xxx", "data": null}' @("/add_data", methods=["POST"]) @ApiDoc.change_doc({"return_json": return_json_str}) def add_data(): """Add some data @@@ ### return ```json return_json ``` @@@ """ return jsonify({"api": "add data"}) @("/delete_data", methods=["GET"]) @ApiDoc.change_doc({"return_json": return_json_str}) def delete_data(): """Delete some data return_json """ return jsonify({"api": "delete data"})
debuggers
Above is Flask-Docs automatically generate Api documents to install and use the tutorials in detail, more information about Flask-Docs generate Api documents please pay attention to my other related articles!