Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open more actions menu

Repository files navigation

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

Silver sponsors

FastAPI

Bronze sponsors

Nixtla


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

And 4 more private sponsor(s).

About

Utilities to execute code blocks in Markdown files.

Topics

Resources

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

Sponsor this project

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.