Sphinx AutoModAPI

0.22.0 · active · verified Thu Apr 16

Sphinx AutoModAPI is an extension for Sphinx that simplifies the automatic generation of API documentation for entire Python modules and packages. It integrates with Sphinx's autodoc and autosummary to create comprehensive API reference pages and summary tables. The current version is 0.22.0, and it maintains a regular release cadence, often aligning with new Python and Sphinx versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart code creates a minimal Python package and a Sphinx documentation project. It configures `conf.py` to enable `sphinx_automodapi.automodapi` and `sphinx_automodapi.automodsumm`, and creates an `index.rst` file that uses `automodapi` to document `my_package.my_module`. Run this Python script, then navigate into the `docs` directory and use `sphinx-build` to generate the HTML documentation.

import os
import shutil

# 1. Create dummy package structure
package_dir = 'my_package'
docs_dir = 'docs'

shutil.rmtree(docs_dir, ignore_errors=True)
shutil.rmtree(package_dir, ignore_errors=True)

os.makedirs(os.path.join(package_dir, 'submodule'), exist_ok=True)
os.makedirs(docs_dir, exist_ok=True)

# 2. Create a dummy Python module for documentation
with open(os.path.join(package_dir, '__init__.py'), 'w') as f: f.write('')
with open(os.path.join(package_dir, 'my_module.py'), 'w') as f:
    f.write('''
def my_function(arg1, arg2):
    """A sample function.

    :param arg1: First argument.
    :param arg2: Second argument.
    :return: The sum of arg1 and arg2.
    """
    return arg1 + arg2

class MyClass:
    """A sample class.

    :param name: The name for the class instance.
    """
    def __init__(self, name):
        self.name = name

    def greet(self):
        """Greets the user.
        """
        return f"Hello, {self.name}!"
''')

# 3. Create Sphinx conf.py
with open(os.path.join(docs_dir, 'conf.py'), 'w') as f:
    f.write(f'''
import os
import sys
sys.path.insert(0, os.path.abspath('..')) # Add parent directory to path for module discovery

project = 'My Awesome Project'
copyright = '2023, Your Name'
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.napoleon', # Recommended for Google/NumPy style docstrings
    'sphinx_automodapi.automodapi',
    'sphinx_automodapi.automodsumm' # For generating summary tables
]
html_theme = 'alabaster'

# REQUIRED for sphinx-automodapi
automodapi_toctreedir = '_autosummary'
''')

# 4. Create an RST file to use automodapi
with open(os.path.join(docs_dir, 'index.rst'), 'w') as f:
    f.write(f'''
.. automodapi:: my_package.my_module
   :no-inherited-members:

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   _autosummary/my_package.my_module
''')

print("Setup complete!\n")
print("To build the documentation:\n")
print(f"1. Navigate to the '{docs_dir}' directory: cd {docs_dir}")
print("2. Run Sphinx build command: sphinx-build -b html . _build")
print("3. Open _build/index.html in your browser.")
print("Note: You need Sphinx installed (pip install sphinx) to run sphinx-build.")

view raw JSON →