Skip to main content

Utilities to execute code blocks in Markdown files.

Project description

Markdown Exec

ci documentation pypi version gitter

Utilities to execute code blocks in Markdown files.

For example, you write a Python code block that computes some HTML, and this HTML is injected in place of the code block.

Installation

pip install "markdown-exec[ansi]"

The ansi extra provides the necessary bits (pygments-ansi-color and a CSS file) to render ANSI colors in HTML code blocks. The CSS file is automatically added to MkDocs' extra_css when Markdown Exec is activated via plugins (see below).

Configuration

This extension relies on the SuperFences extension of PyMdown Extensions.

To allow execution of code blocks, configure a custom fence from Python:

from markdown import Markdown
from markdown_exec import formatter, validator

Markdown(
    extensions=["pymdownx.superfences"],
    extension_configs={
        "pymdownx.superfences": {
            "custom_fences": [
                {
                    "name": "python",
                    "class": "python",
                    "validator": validator,
                    "format": formatter,
                }
                # ...one fence for each language we support:
                # bash, console, md, markdown, py, python, pycon, sh, tree
            ]
        }
    }
)

...or in MkDocs configuration file, as a Markdown extension:

# mkdocs.yml
markdown_extensions:
- pymdownx.superfences:
    custom_fences:
    - name: python
      class: python
      validator: !!python/name:markdown_exec.validator
      format: !!python/name:markdown_exec.formatter
    # ...one fence for each language we support:
    # bash, console, md, markdown, py, python, pycon, sh, tree

...or in MkDocs configuration file, as a plugin:

# mkdocs.yml
plugins:
- search
- markdown-exec

# SuperFences must still be enabled!
markdown_extensions:
- pymdownx.superfences

We do recommend enabling Markdown Exec with the MkDocs plugin if you are using MkDocs: it will take care of adding relevant assets (CSS/JS) to the final site when needed.

Usage

You are now able to execute code blocks instead of displaying them:

```python exec="on"
print("Hello Markdown!")
```

The exec option will be true for every possible value except 0, no, off and false (case insensitive).

Below you can see an example of running a bash script that is expected to return a non-zero exit code:

```bash exec="1" source="tabbed-left" returncode="2"
grep extra_css README.md && exit 2
```

See usage for more details, and the gallery for more examples!

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

markdown_exec-1.11.0.tar.gz (81.8 kB view details)

Uploaded Source

Built Distribution

markdown_exec-1.11.0-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file markdown_exec-1.11.0.tar.gz.

File metadata

  • Download URL: markdown_exec-1.11.0.tar.gz
  • Upload date:
  • Size: 81.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for markdown_exec-1.11.0.tar.gz
Algorithm Hash digest
SHA256 e0313a0dff715869a311d24853b3a7ecbbaa12e74eb0f3cf7d91401a7d8f0082
MD5 ee710d4a077a5abb4f900efe9a25e2d1
BLAKE2b-256 e8e4ddd5ca350f2b072e51a22359cb51e94b5fdbe85810351e7484ccfd923324

See more details on using hashes here.

File details

Details for the file markdown_exec-1.11.0-py3-none-any.whl.

File metadata

  • Download URL: markdown_exec-1.11.0-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for markdown_exec-1.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0526957984980f55c02b425d32e8ac8bb21090c109c7012ff905d3ddcc468ceb
MD5 ca6de4fddd085d91056dfe5a2ecc55aa
BLAKE2b-256 22415551f05c0e6430e3d2dcbd40965840a4cf280c045a529552690f04b7c0a0

See more details on using hashes here.

Supported by

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