Skip to main content

Utilities to execute code blocks in Markdown files.

Project description

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

TIP: We 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.

WARNING: Limitation of configuration through Markdown/PyMDown. Configuration through pymdownx.superfences directly is not well supported 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.

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

Silver sponsors

Material for MkDocs
FastAPI
Pydantic

Bronze sponsors

Nixtla


ofek samuelcolvin tlambert03 ssbarnea femtomc cmarqu kolenaIO ramnes machow BenHammersley trevorWieland laenan8466 MarcoGorelli analog-cbarber OdinManiac rstudio-sponsorship schlich SuperCowPowers butterlyn livingbio NemetschekAllplan EricJayHartman 15r10nk cdwilson activeloopai roboflow wrath-codes leodevian cmclaughlin blaisep RapidataAI rodolphebarbanneau theSymbolSyndicate blakeNaccarato ChargeStorm Alphadelta14

And 8 more private sponsor(s).

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.12.1.tar.gz (78.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_exec-1.12.1-py3-none-any.whl (38.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: markdown_exec-1.12.1.tar.gz
  • Upload date:
  • Size: 78.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for markdown_exec-1.12.1.tar.gz
Algorithm Hash digest
SHA256 eee8ba0df99a5400092eeda80212ba3968f3cbbf3a33f86f1cd25161538e6534
MD5 7937943421cad50c85faeb36581c6288
BLAKE2b-256 96731f20927d075c83c0e2bc814d3b8f9bd254d919069f78c5423224b4407944

See more details on using hashes here.

File details

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

File metadata

  • Download URL: markdown_exec-1.12.1-py3-none-any.whl
  • Upload date:
  • Size: 38.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for markdown_exec-1.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a645dce411fee297f5b4a4169c245ec51e20061d5b71e225bef006e87f3e465f
MD5 335ac67a574608e42f153d2ffbe2e6c8
BLAKE2b-256 ea227b684ddb01b423b79eaba9726954bbe559540d510abc7a72a84d8eee1b26

See more details on using hashes here.

Supported by

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