Flask-Docs
Adds Docs support to Flask.
English | 简体中文
Features
- Automatic generation of markdown documentation
- Support offline markdown document download
- Support Flask-RESTful
- Support Flask-RESTX
- Support Flask MethodView
- Support online debugging
- Support command to generate offline document
- HTML
- Markdown
Installation
pip3 install Flask-Docs
Usage
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",
)
View the documentation page
http://127.0.0.1/docs/api/
Page demo
Configuration
# Using CDN
# app.config["API_DOC_CDN"] = True
# Disable document pages
# app.config["API_DOC_ENABLE"] = False
# SHA256 encrypted authorization password, e.g. here is admin
# echo -n admin | shasum -a 256
# app.config["API_DOC_PASSWORD_SHA2"] = "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
# Methods allowed to be displayed
# app.config["API_DOC_METHODS_LIST"] = ["GET", "POST", "PUT", "DELETE", "PATCH"]
# Custom url_prefix
# app.config["API_DOC_URL_PREFIX"] = "/docs/api"
# RESTful API class name to exclude
# app.config["API_DOC_RESTFUL_EXCLUDE"] = ["Todo"]
# Name of the API blueprint to be displayed
# app.config["API_DOC_MEMBER"] = ["api", "platform"]
# Name of the Submembers API function to be excluded
# app.config["API_DOC_MEMBER_SUB_EXCLUDE"] = ["delete_data"]
# Auto generating request args markdown
# app.config["API_DOC_AUTO_GENERATING_ARGS_MD"] = True
# Disable markdown processing for all documents
# app.config["API_DOC_ALL_MD"] = False
Tag @@@
# Process all documents in markdown by default
# 1. use the `@@@` wrapper if you want to specify processing
# 2. Turn off `API_DOC_ALL_MD` and remove the `@@@` tag if you want to display the original document
@@@
# Write your markdown document here
@@@
API
@api.route("/add_data", methods=["POST"])
def add_data():
"""Add some data
### args
| args | required | request type | type | remarks |
|-------|----------|--------------|------|----------|
| title | true | body | str | blog title |
| name | true | body | str | person's name |
### request
```json
{"title": "xxx", "name": "xxx"}
```
### return
```json
{"code": xxxx, "msg": "xxx", "data": null}
```
"""
return jsonify({"api": "add data"})
app.register_blueprint(api, url_prefix="/api")
@api.route("/delete_data", methods=["GET"])
def delete_data():
"""Delete some data
@@@
### args
| args | required | request type | type | remarks |
|--------|----------|--------------|------|--------------|
| id | false | query | str | blog id |
| name | true | 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"})
app.register_blueprint(api, url_prefix="/api")
@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(platform, url_prefix="/platform")
Flask-RESTful API
from flask_restful import Resource, Api
class Todo(Resource):
"""Manage todo"""
def post(self):
"""Add todo
@@@
### description
> Add todo
### args
| args | required | request type | type | remarks |
|-------|----------|--------------|------|----------|
| name | true | body | str | todo name |
| type | true | 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 | required | request type | type | remarks |
|-------|----------|--------------|------|----------|
| name | true | query | str | todo name |
| type | false | 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 = Api(app)
restful_api.add_resource(Todo, "/todo")
Flask MethodView API
For the time being, only url_rule with the same class name are supported
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"))
Decorator @ApiDoc.change_doc
Reuse comments
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"})
Debugger
Command to generate offline document
- HTML: Run
flask docs htmlwill generate offline html document athtmldoc/ - Markdown: Run
flask docs markdownwill generate thedoc.mdoffline markdown document
Examples
Thanks
Metadata
Release files for Flask-Docs 0.7.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask_docs-0.7.6.tar.gz | 542.4 kB | Without provenance | Details for flask_docs-0.7.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Flask_Docs-0.7.6-py3-none-any.whl | Python 3 | none | any | Details for Flask_Docs-0.7.6-py3-none-any.whl |
Total release size: 1.1 MB
Release files / flask_docs-0.7.6.tar.gz
| Download URL | flask_docs-0.7.6.tar.gz |
|---|---|
| Size | 542.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
df579c9264c806ca484ba612041a0b0bb550109ecedf3a0ee252bffc486fc2a3
|
|
BLAKE2b-256 checksum How to use checksums |
b3041595d57e735fad536aa3d04f23b0830a6b75fe7f50de5cd731924a2d647d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / Flask_Docs-0.7.6-py3-none-any.whl
| Download URL | Flask_Docs-0.7.6-py3-none-any.whl |
|---|---|
| Size | 541.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c0e1ae06e8879113d1a2ef1b4f476f73ced8dffba014d2a1379d4f51daf6391e
|
|
BLAKE2b-256 checksum How to use checksums |
cab8be216d59e83682ac931b25f0c06cf8614384b47a3504f6f4fc21cdc140a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Security for Flask-Docs 0.7.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.