Skip to main content

Extract and process code blocks from Markdown files.

Project description


Extract and process code blocks from Markdown files. Now you can keep code examples automatically:

  • formatted (e.g. using black for Python)
  • type checked
  • unit tested
  • linted
  • up-to-date with --help
  • etc


With pip:

pip install codeblocks

With poetry:

poetry add codeblocks


Usage: codeblocks [OPTIONS] LANGUAGE FILE [COMMAND]...

  Extract or process LANGUAGE code blocks in Markdown FILE.

  Extract Python code blocks:
      codeblocks python

  Reformat Python code blocks using black, in place:
      codeblocks python -- black -

  --wrap   Wrap each code block in a function.
  --check  Do not modify the file, just return the status. Return code 0 means
           block matches the command output. Return code 1 means block would
           be modified.

  --help   Show this message and exit.


Extract Python code blocks:

codeblocks python

Check formatting of Python code blocks with black:

codeblocks --check python -- black -

Reformat Python code blocks with black, in place:

codeblocks python -- black -

Type check Python code blocks with mypy (--wrap puts each code block into its own function):

mypy somemodule anothermodule <(codeblocks python --wrap

Insert the output of codeblock --help into usage block in this

codeblocks usage -- codeblocks --help

Check that usage block in this is up-to-date with --help output:

codeblocks --check usage -- codeblocks --help

Full type checking example

def plus(x: int, y: int) -> int:
    return x + y

plus(1, '2')
$ mypy --pretty --strict <(codeblocks python
/dev/fd/63:5: error: Argument 2 to "plus" has incompatible type "str"; expected "int"
        plus(1, '2')
Found 1 error in 1 file (checked 1 source file)


There are alternative tools, but none of them supported all of the cases above.

Project details

Download files

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

Source Distribution

codeblocks-0.2.3.tar.gz (8.1 kB view hashes)

Uploaded source

Built Distribution

codeblocks-0.2.3-py3-none-any.whl (8.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page