Skip to main content

Markdown Exec

ci documentation pypi version gitter radicle

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, pyodide, sh, tree, etc.
            ]
        }
    }
)

...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

...or in Zensical configuration file, as a legacy plugin:

[project.plugins.markdown-exec]

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

WARNING: Limitation of configuration through Markdown/PyMDown for MkDocs. Configuration through pymdownx.superfences directly is not well supported within MkDocs for fences that require assets inclusion, like pyodide. For these you will have to include the assets manually. In the future we will provide ways to include them automatically. In Zensical these assets are provided natively.

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!

Sponsors

Bronze sponsors

Nixtla


ofek samuelcolvin tlambert03 ssbarnea femtomc FastAPI cmarqu kolenaIO ramnes machow BenHammersley trevorWieland MarcoGorelli analog-cbarber OdinManiac rstudio-sponsorship schlich butterlyn livingbio NemetschekAllplan EricJayHartman 15r10nk activeloopai roboflow RapidataAI rodolphebarbanneau theSymbolSyndicate blakeNaccarato ChargeStorm Cusp-AI

And 4 more private sponsor(s).

Metadata

Release files for markdown-exec 1.12.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markdown-exec 1.12.6
File Size Uploaded Provenance Actions
markdown_exec-1.12.6.tar.gz 84.5 kB Without provenance Details for markdown_exec-1.12.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for markdown-exec 1.12.6
File Interpreter ABI Platform Actions
markdown_exec-1.12.6-py3-none-any.whl Python 3 none any Details for markdown_exec-1.12.6-py3-none-any.whl

Total release size: 139.5 kB

Release files / markdown_exec-1.12.6.tar.gz

Download URL markdown_exec-1.12.6.tar.gz
Size 84.5 kB
Tags Source
SHA-256 checksum
How to use checksums
31d4070b3137395fbea0f5e82f21226f9919debbbb7fc3dfcfff0c660cc64ae1
BLAKE2b-256 checksum
How to use checksums
da9602cd0802bc17b4e9b818b07b44096d40ce57d9699c6d35e72f25c5b61dd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Release files / markdown_exec-1.12.6-py3-none-any.whl

Download URL markdown_exec-1.12.6-py3-none-any.whl
Size 55.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2dabe3c209f10f25aac2585bb8cdfcf325df691d4054f3394fba4b8c87ae4ee
BLAKE2b-256 checksum
How to use checksums
c9b5ac82d68378beb338fa5acc5b2b8f08f3abc844d864c0ce2456da57a26d47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Security for markdown-exec 1.12.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.12.6 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page