I'm looking for a quick way to auto produce REST API docs from a Flask REST API I've written. Does anyone know of tools that can do this and how I would markup the code?
3 Answers
I would recommend you Sphinx, you add your documentation as __doc__
and the autodoc
module of Sphinx will generate the docs for you (docs.python.org also uses Sphinx). The markup is reStructuredText, similiar to Markdown (if you prefer Markdown, you can use pdoc).
e.g.:
@app.route('/download/<int:id>')
def download_id(id):
'''This downloads a certain image specified by *id*'''
return ...
-
1Maybe I'm a newb, but don't get the appeal of sphinx. The documentation page is really busy and seems like the docs are completely external to the app, rather than other approaches which just instrument a pre-existing flask app with decorators and autogenerate stuff.... – Adam Hughes Jul 01 '20 at 14:59
I really like Swagger because it allows to generate an API documentation by just adding a few decorators and comments into your code. There is a Flask Swagger available.
from flask import Flask
from flask.ext.restful import Api
from flask_restful_swagger import swagger
app = Flask(__name__)
api = swagger.docs(Api(app), apiVersion='1', api_spec_url="/api/v1/spec")
class Unicorn(Resource):
"Describing unicorns"
@swagger.operation(
notes='some really good notes'
)
def get(self, todo_id):
...
Then you can see your methods and notes in an html interface just by visiting /api/v1/spec (it serves needed static automatically). You can also just get all your API description in JSON and parse it otherwise.

- 2,320
- 18
- 13
-
1
-
1The importance of this question in stark contrast with this communities eagerness to close questions is a perfect example of what needs to be addressed here. This is a great question for which I am looking for an answer to, and I want to ask the author to expand on the question but I can't. This question is not an MWE and it's not clear what, for example, `@swagger` is. – puk Mar 18 '21 at 01:15
There is a Flask extension: flask-autodoc for auto documentation specially parsing endpoint route rule. You can add doc
decorator to specify which APIs you want to doc:
@app.route('/doc')
@auto.doc()
def documentation():
'''
return API documentation page
'''
return auto.html()
@app.route('/')
@auto.doc()
def welcome():
'''
Welcome API
'''
commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"])
commit_msg = subprocess.check_output(["git", "log", "-1", "--format=%s"])
date_time = subprocess.check_output(["git", "log", "-1", "--format=%cd"])
return "Welcome to VM Service Server. <br/>" \
"The last commit: %s<br/>Date: %s, <br>Hash: %s" % \
(commit_msg, date_time, commit_hash), 200
The simple html documentation page is like this:

- 4,353
- 4
- 39
- 42
-
16I realize it is kind of an opinion, but I would warn people away from flask-autodoc. The extension is really incomplete. It starts off great, and it sets up the way you would expect, but the end result is lackluster. Most people will abandon it for sphinx and will have wasted a few hours on flask-autodoc. – melchoir55 Apr 29 '15 at 06:17
-
3
-
Could you elaborate why flask-autodoc is incomplete? I took a quick look at the documentation for it, and I liked it a lot more than e.g., Flask-RESTplus. – imolit Jul 24 '17 at 12:35
-
-
1I found this thread today, and this solution looked very promising. Unfortunately it is not maintained anymore, but I was able to find a well maintained fork of it here: https://github.com/jwg4/flask-selfdoc – tycl Jul 05 '20 at 22:22