Utilities to execute code blocks in Markdown files.
Actively maintained. Last release 11 days ago, commits in 9 of the last 26 weeks. A safe choice.
pip install markdown-execMarkdown Exec
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:
