Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Sphinx auto flask to document flask-restful API?

I have a flask app that I want to use Sphinx's autoflask directive to document a flask-restful API.

https://pythonhosted.org/sphinxcontrib-httpdomain/#module-sphinxcontrib.autohttp.flask

I have installed the module via pip and run sphinx-quickstart, which gives me a conf.py and index.rst.

I've tried putting the extension into conf.py:

extensions = ['sphinxcontrib.autohttp.flask']

and the directive into index.rst as per the documentation:

.. autoflask:: autoflask_sampleapp:app
:undos-static:

But I can't get the app:module (autoflask_sampleapp:app) part correct. As a result, when I run sphinx-build I get an error that either the app or the module are not found.

My app trees looks like this:

.
├── admin
├── apis
├── app
│   ├── static
│   └── templates

and from the app's root directory, I can say:

from apis import profile

How do I configure auto flask in the index.rst to correctly find and load my app's API modules?

like image 976
David Watson Avatar asked Jan 25 '16 19:01

David Watson


1 Answers

My code structure, where application.py file with flask app, I run my server python appllication.py runserver

├── application.py
├── _build
│   ├── doctrees
│   │   ├── environment.pickle
│   │   └── index.doctree
│   └── html
│       ├── genindex.html
│       ├── http-routingtable.html
│       ├── index.html
│       ├── objects.inv
│       ├── search.html
│       ├── searchindex.js
│       ├── _sources
│       │   └── index.txt
│       └── _static
├── conf.py
├── index.rst

In conf.py you should include extensions and include abs path to you application.py or any other main flask app file in your project.

import os
import sys
sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinxcontrib.autohttp.flask',
    'sphinxcontrib.autohttp.flaskqref'
]

After you can use blueprints, views from your flask app

My documentation!
=======================================


.. qrefflask:: application:application
   :undoc-static:

=======================================
Api details!
=======================================

.. autoflask:: application:application
   :undoc-static:

In other words, before run make html, you should add abs path to you root application folder via python sys path sys.path.insert(0, os.path.abspath('/home/myproject/')), where /home/myproject folder with your source code.

like image 89
VelikiiNehochuha Avatar answered Nov 15 '22 08:11

VelikiiNehochuha