Skip to main content

Run `black` on python code blocks in documentation files

Project description

Build Status Azure DevOps coverage status


Run black on python code blocks in documentation files.


pip install blacken-docs


blacken-docs provides a single executable (blacken-docs) which will modify .rst / .md / .tex files in place.

It currently supports the following black options:

  • -l / --line-length
  • -t / --target-version
  • -S / --skip-string-normalization

Following additional parameters can be used:

  • -E / --skip-errors

blacken-docs will format code in the following block types:


    def hello():
        print("hello world")


    .. code-block:: python

        def hello():
            print("hello world")

(rst pycon)

    .. code-block:: pycon

        >>> def hello():
        ...     print("hello world")


def hello():
    print("hello world")

(latex with pythontex)

def hello():
    print("hello world")

(markdown/rst in python docstrings)

def f():
    """docstring here

    .. code-block:: python

        print("hello world")

    print("hello world")

usage with pre-commit

See pre-commit for instructions

Sample .pre-commit-config.yaml:

-   repo:
    rev: v1.10.0
    -   id: blacken-docs
        additional_dependencies: [black==...]

Since black is currently a moving target, it is suggested to pin black to a specific version using additional_dependencies.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for blacken-docs, version 1.10.0
Filename, size File type Python version Upload date Hashes
Filename, size blacken_docs-1.10.0-py2.py3-none-any.whl (5.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size blacken_docs-1.10.0.tar.gz (5.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page