欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flask-Docs自動生成Api文檔安裝使用教程

 更新時間:2023年10月16日 11:47:33   作者:kwkwc  
這篇文章主要為大家介紹了Flask-Docs自動生成Api文檔安裝使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Flask-Docs

影響我寫文檔的原因可能是代碼和文檔分離,有時候?qū)懲甏a會忘記補(bǔ)文檔,而且不能及時查看,使用 Flask-Docs 可以解決我的問題,這個插件可以根據(jù)代碼注釋生成文檔頁面,代碼注釋改動文檔可以及時更新,而且支持離線文檔下載,支持在線調(diào)試。

Flask-Docs  Flask Api 文檔自動生成插件

特性

  • 根據(jù)代碼注釋自動生成文檔
  • 支持離線 markdown 文檔下載
  • 支持 Flask-RESTful
  • 支持 Flask-RESTX
  • 支持 flask.views.MethodView
  • 支持在線調(diào)試

鏈接 https://github.com/kwkwc/flas...

安裝

pip3 install Flask-Docs

使用

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",
)

配置

# 使用 CDN
# app.config["API_DOC_CDN"] = True
# 禁用文檔頁面
# app.config["API_DOC_ENABLE"] = False
# SHA256 加密的授權(quán)密碼,例如這里是 admin
# echo -n admin | shasum -a 256
# app.config["API_DOC_PASSWORD_SHA2"] = "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
# 允許顯示的方法
# app.config["API_DOC_METHODS_LIST"] = ["GET", "POST", "PUT", "DELETE", "PATCH"]
# 自定義 url_prefix
# app.config["API_DOC_URL_PREFIX"] = "/docs/api"
# 需要排除的 RESTful Api 類名
# app.config["API_DOC_RESTFUL_EXCLUDE"] = ["Todo"]
# 需要顯示的 Api 藍(lán)圖名稱
# app.config["API_DOC_MEMBER"] = ["api", "platform"]
# 需要排除的子成員 Api 函數(shù)名稱
# app.config["API_DOC_MEMBER_SUB_EXCLUDE"] = ["delete_data"]

如何書寫 markdown 格式文檔

@@@
在注釋結(jié)尾用 "@@@" 包含 markdown 格式文檔
@@@

查看文檔頁面

http://127.0.0.1/docs/api/

Api demo

@api.route("/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"})

@api.route("/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"})

@platform.route("/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(requests.get(url).text)
    except:
        pass
    ```
    ### return
    ```json
    {"code": xxxx, "msg": "xxx", "data": null}
    ```
    @@@
    """
    return jsonify({"platform": "get something"})

完整代碼

from flask import Blueprint, Flask, jsonify
from flask_docs import ApiDoc
app = Flask(__name__)
app.config["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__)
@api.route("/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"})
@api.route("/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"})
@platform.route("/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(requests.get(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__":
    app.run(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__":
    app.run(host="0.0.0.0", port=5000, debug=True)

flask.views.MethodView Api demo

目前只支持與類名相同的 url_rule

from flask.views 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"))

裝飾器 @ApiDoc.change_doc

復(fù)用注釋

from flask_docs import ApiDoc
return_json_str = '{"code": xxxx, "msg": "xxx", "data": null}'
@api.route("/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"})
@api.route("/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"})

調(diào)試器

以上就是Flask-Docs自動生成Api文檔安裝使用教程的詳細(xì)內(nèi)容,更多關(guān)于Flask-Docs生成Api文檔的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python pickle 和 shelve模塊的用法

    python pickle 和 shelve模塊的用法

    pickle和shelve模塊都可以把python對象存儲到文件中,下面來看看它們的用法吧
    2013-09-09
  • Python3實現(xiàn)監(jiān)控新型冠狀病毒肺炎疫情的示例代碼

    Python3實現(xiàn)監(jiān)控新型冠狀病毒肺炎疫情的示例代碼

    這篇文章主要介紹了Python3實現(xiàn)監(jiān)控新型冠狀病毒肺炎疫情的示例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 深入理解最新Python中的Match Case

    深入理解最新Python中的Match Case

    最近發(fā)布的 Python 3.10 的所有主要新特性中最重要就是 Match-Case 語法,本文將帶你深入探討,會發(fā)現(xiàn) Match-Case 其實沒有那么簡單
    2021-11-11
  • 使用Python實現(xiàn)屏幕截圖功能的詳細(xì)教程

    使用Python實現(xiàn)屏幕截圖功能的詳細(xì)教程

    Python使用ImageGrab截圖主要依賴于Pillow庫(PIL庫的一個分支),該庫提供了ImageGrab模塊來實現(xiàn)屏幕截圖功能,以下是一個詳細(xì)的截圖教程,需要的朋友可以參考下
    2025-01-01
  • 詳解Python import方法引入模塊的實例

    詳解Python import方法引入模塊的實例

    這篇文章主要介紹了詳解Python import方法引入模塊的實例的相關(guān)資料,在Python用import或者from…import或者from…import…as…來導(dǎo)入相應(yīng)的模塊,需要的朋友可以參考下
    2017-08-08
  • django驗證系統(tǒng)的具體使用

    django驗證系統(tǒng)的具體使用

    本文主要介紹了django驗證系統(tǒng)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 利用Python柵格化地圖(以成都市為例,含代碼)

    利用Python柵格化地圖(以成都市為例,含代碼)

    這篇文章主要給大家介紹了關(guān)于利用Python柵格化地圖的相關(guān)資料,
    Python中可以使用多種庫來進(jìn)行柵格化地圖的操作,其中比較常用的有g(shù)eopandas、rasterio等,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • pd.read_csv讀取文件路徑出現(xiàn)的問題解決

    pd.read_csv讀取文件路徑出現(xiàn)的問題解決

    本文主要介紹了pd.read_csv讀取文件路徑出現(xiàn)的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 關(guān)于Python自動化操作Excel

    關(guān)于Python自動化操作Excel

    這篇文章主要介紹了關(guān)于Python自動化操作Excel, Python 是一種功能強(qiáng)大的編程語言,可以用于許多任務(wù),包括處理 Excel 文件,需要的朋友可以參考下
    2023-04-04
  • 一文帶你搞懂Python中的pyc文件

    一文帶你搞懂Python中的pyc文件

    Python是一門解釋性語言,沒有嚴(yán)格意義上的編譯和匯編過程。Pyc文件是py編譯過程中產(chǎn)生的字節(jié)碼文件,可以由虛擬機(jī)直接執(zhí)行,是python將目標(biāo)源碼編譯成字節(jié)碼以后在磁盤上的文件形式。本文就來聊聊pyc文件的寫入等只是,感興趣的可以了解一下
    2022-12-12

最新評論