markdown-exec

1.12.1 · active · verified Sun Apr 12

markdown-exec is a Python library that provides utilities to execute code blocks directly within Markdown files. It's commonly used with MkDocs and PyMdown-Extensions to inject dynamic content, such as script outputs or interactive elements, into documentation. The library sees active development with releases typically occurring every few months.

Warnings

Install

Imports

Quickstart

To quickly get started, create an `mkdocs.yml` file and a Markdown file (e.g., `docs/index.md`). Enable the `markdown-exec` plugin and `pymdownx.superfences` extension in your `mkdocs.yml`. Then, add a code block with `exec="on"` in your Markdown file. The example illustrates the structure for MkDocs integration, which is the recommended approach.

import os

# mkdocs.yml configuration example
# plugins:
#   - search
#   - markdown-exec
# markdown_extensions:
#   - pymdownx.superfences

# Example Markdown content (e.g., in docs/index.md)
markdown_content = '''
# My Executable Documentation

This is a simple example of markdown-exec in action.

```python exec="on"
import sys
print(f"Hello from Python {sys.version.split(' ')}!")
```
'''

# To simulate execution without a full MkDocs setup, one would typically process this
# via the Markdown library with the markdown-exec extension enabled.
# However, the primary intended use is within MkDocs.
# For a true quickstart, an MkDocs project is implicitly assumed.
# This example is illustrative of the Markdown content.

# To make it runnable for an agent, assume an MkDocs build process:
# 1. Create a dummy mkdocs.yml (if not present)
# 2. Create a dummy docs/index.md with the content above
# 3. Run `mkdocs build` or `mkdocs serve`

# This Python snippet shows how the markdown content looks, not how to run it directly in Python for output capture.
# The actual execution happens via the Markdown extension or MkDocs plugin.

view raw JSON →