pdoc Documentation

repository·main·Indexed 25 days ago

https://github.com/mitmproxy/pdoc

pdoc is a tool for generating API documentation for Python projects. It supports modern Python features like type annotations, numpydoc and Google-style docstrings, and outputs standalone HTML. Key features include a built-in web server with live reloading, customizable Jinja2 templates, and the ability to be used as either a CLI tool or a Python library. It provides integration paths for static site generators like MkDocs and Hugo.

Tokens
10.9K
Snippets
15
Records
87
Agent score
77%

What's inside pdoc

  1. Key features of pdoc

    main

    pdoc is designed for simplicity and focuses on modern Python documentation tasks:

    • Markdown Support: Documentation is written in plain Markdown.
    • Modern Python Support: First-class support for type annotations and other modern Python 3 features.
    • Live Reloading: Includes a builtin web server with live reloading for development.
    • Customizable Templates: Supports customizable HTML templates.
    • Docstring Formats: Understands both numpydoc and Google-style docstrings.
    • Standalone Output: Generates standalone HTML output without requiring additional dependencies.

    Under the hood behavior:

    • Automatically links identifiers in docstrings to their corresponding documentation.
    • Respects the __all__ variable when present.
    • Traverses the AST to extract type annotations and docstrings from constructors.
    • Automatically resolves type annotation string literals as forward references.
    • Uses inheritance to resolve type annotations and docstrings for class members.
  2. Configure linking and TOC when using pdoc with mkdocs

    main

    When integrating pdoc with mkdocs, be aware of the following technical constraints:

    Cross-page Linking

    If you need to link between different pages in your documentation, you must set use_directory_urls: false in your mkdocs.yml configuration. Otherwise, pdoc's generated links may not resolve correctly.

    Table of Contents (TOC)

    pdoc does not automatically populate the mkdocs Table of Contents because mkdocs' Markdown parser ignores the raw HTML generated by pdoc. To prevent a broken or empty TOC from appearing, you may need to hide the mkdocs table of contents using custom CSS within your frame.html.jinja2 template.

  3. Use a custom template with pdoc

    main

    You can override pdoc's default documentation templates by providing a directory containing your own template files. Use the --template-directory or the short -t command line argument followed by the path to your template directory.

    When using a custom template directory, pdoc will look for templates within that directory to match its internal lookup logic.

    pdoc -t ./examples/custom-template pdoc
  4. Extend pdoc templates

    main

    pdoc allows you to customize the look and feel of generated documentation by providing your own template directory. pdoc follows a lookup priority: it checks for a template file in your custom directory before falling back to the default/ directory.

    To extend a default template rather than replacing it entirely, use the Jinja2 {% extends %} tag. This allows you to override specific blocks (like title) while keeping the rest of the default structure.

    {% extends "default/module.html.jinja2" %}
    {% block title %}new page title{% endblock %}
  5. Generate API documentation with pdoc

    main

    You can use pdoc to document a Python module or a specific file.

    To document a module:

    pdoc your_python_module

    To document a specific file:

    pdoc ./my_project.py

    To generate standalone HTML output to a specific directory (e.g., ./html):

    pdoc -o ./html pdoc
  6. Explore pdoc template examples

    main

    The examples/ directory provides several patterns for using and customizing pdoc:

    • custom-template: Demonstrates various template configuration options.
    • dark-mode: Shows how to apply a dark theme to pdoc styles.
    • library-usage: Demonstrates how to use pdoc as a Python library rather than a CLI tool.
    • mkdocs: Provides a guide for integrating pdoc with the MkDocs static site generator.
  7. Integrate pdoc with mkdocs

    main

    To use pdoc within an mkdocs site, you can generate HTML documentation using a custom template and then treat the output as Markdown files. Since mkdocs' Markdown parser accepts interspersed HTML, this allows you to leverage pdoc's API extraction while using mkdocs for site structure and hosting.

    Workflow

    1. Create a custom frame.html.jinja2 template to strip away pdoc's standard HTML wrappers, leaving only the core documentation content.
    2. Run pdoc to generate the documentation.
    3. Rename the resulting output files from .html to .md.
    4. Run mkdocs serve to view the site.

    Note: In this specific example repository, you can automate this by running ./make.py to generate the API docs and then mkdocs serve to view the website.

    ./make.py
    mkdocs serve
  8. Configure syntax highlighting styles

    main

    pdoc uses Pygments for syntax highlighting. The styles are defined in syntax-highlighting.css. You can generate a custom version of this file using the pygmentize command.

    To generate a new stylesheet, use the following command, replacing <theme> with your desired Pygments theme name:

    pygmentize -f html -a .pdoc-code -S <theme> > default/syntax-highlighting.css
  9. Implement dark mode in pdoc using custom stylesheets

    main

    To implement a dark mode theme in pdoc, you must override the default CSS variables and the Pygments syntax highlighting theme. pdoc defines its color scheme using CSS variables, which allows you to customize the appearance by providing a custom theme.css and a custom syntax-highlighting.css file.

    To preview a dark mode implementation locally, navigate to the example directory and run pdoc using the -t (template) flag pointing to the current directory.

    cd examples/dark-mode && pdoc -t . pdoc