pdoc: API Documentation Generator

16.0.0 · active · verified Fri Apr 10

pdoc is a Python package for generating API documentation that follows your project's Python module hierarchy. It extracts documentation from docstrings, supports Markdown, type annotations, and various docstring formats (NumPy, Google-style). It can generate standalone HTML files or serve documentation via a built-in live-reloading web server, focusing on simplicity and minimal configuration. It is actively maintained by the mitmproxy team.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate HTML documentation for a Python module programmatically using `pdoc.html()`. It creates a dummy module, generates its documentation, and saves it to an HTML file. The `pdoc` library is primarily used as a command-line tool, but offers a robust programmatic API. For CLI usage, simply run `pdoc your_module.py` or `pdoc your_package`.

import os
import sys
from pathlib import Path

# Create a dummy module for documentation
dummy_module_content = '''
"""A simple example module."""

class MyClass:
    """This is MyClass.

    Attributes:
        name (str): The name of the instance.
    """
    def __init__(self, name: str):
        self.name = name

    def greet(self, loud: bool = False) -> str:
        """Greets the user.

        Args:
            loud (bool): If True, returns an uppercase greeting.

        Returns:
            str: The greeting message.
        """
        message = f"Hello, {self.name}!"
        return message.upper() if loud else message

def my_function(x: int, y: int) -> int:
    """Adds two numbers.

    Args:
        x (int): The first number.
        y (int): The second number.

    Returns:
        int: The sum of x and y.
    """
    return x + y
'''

# Create a temporary file for the dummy module
module_path = Path('./temp_my_module.py')
module_path.write_text(dummy_module_content)

# Add the current directory to sys.path to allow importing the temporary module
sys.path.insert(0, str(Path('.').resolve()))

# Generate documentation programmatically
try:
    import temp_my_module
    from pdoc import html

    print("Generating documentation for temp_my_module...")
    # Generate HTML documentation for the module
    html_output = html(temp_my_module)

    # Optionally write to a file
    output_dir = Path('./pdoc_output')
    output_dir.mkdir(exist_ok=True)
    output_file = output_dir / 'temp_my_module.html'
    output_file.write_text(html_output)

    print(f"Documentation generated to {output_file.resolve()}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the temporary module file and sys.path
    if module_path.exists():
        module_path.unlink()
    if str(Path('.').resolve()) in sys.path:
        sys.path.remove(str(Path('.').resolve()))

# You can also run from the command line:
# pdoc ./temp_my_module.py
# or for a package:
# pdoc my_package --output-directory docs

view raw JSON →