Once the extension is configured (see README/Overview), you can execute code blocks by enabling the exec option:
```pythonexec="on"
print("Hello Markdown!")```
The exec option will be true for every possible value except 0, no, off and false (case insensitive).
To enable automatic execution of code blocks for specific languages (without having to add the exec="on" option to your code blocks), set the MARKDOWN_EXEC_AUTO environment variable:
By default, Markdown Exec will render what you print as Markdown. If you want to skip rendering, to inject HTML directly, you can set the html option to true.
System information:
```pythonexec="true"
importplatformfromtextwrapimportdedentprint(# we must dedent, otherwise Markdown# will render it as a code block!dedent(f""" - machine: `{platform.machine()}` - version: `{platform.version()}` - platform: `{platform.platform()}` - system: `{platform.system()}` """))```
System information:
machine: x86_64
version: #1 SMP PREEMPT_DYNAMIC Sun, 23 Mar 2025 17:17:30 +0000
If you are using Python Markdown's toc extension, or writing docs with MkDocs, you will notice that the headings you generated by executing a code block appear in the table of contents. If you don't want those headings to appear in the ToC, you can use the updatetoc="no" boolean option:
```pythonexec="1" updatetoc="no"
print("# XL heading\n")print("## L heading\n")print("### M heading\n")print("#### S heading\n")```
When your executed code blocks output Markdown, this Markdown is rendered to HTML, and every HTML id is automatically prefixed with exec-N--, where N is an integer incremented with each code block. To avoid breaking links, every href attribute is also updated when relevant.
You can change this prefix, or completely remove it with the idprefix option.
The following ids are not prefixed:
```pythonexec="1" idprefix="" updatetoc="no"
print("#### Commands")print("\n[link to commands](#commands)")```
It's possible to render both the result of the executed code block and the code block itself. For this, use the source option with one of the following values:
above: The source code will be rendered above the result.
below: The source code will be rendered below the result.
material-block: The source code and result will be wrapped in a nice-looking block (only works with Material for MkDocs, and requires the md_in_html extension)
tabbed-left: The source code and result will be rendered in tabs, in that order (requires the pymdownx.tabbed extension).
tabbed-right: The result and source code will be rendered in tabs, in that order (requires the pymdownx.tabbed extension).
console: The source and result are concatenated in a single code block, like an interactive console session.
Source above:
```pythonexec="true" source="above"
print("I'm the result!")```
print("I'm the result!")
I'm the result!
Source below:
```pythonexec="true" source="below"
print("I'm the result!")```
I'm the result!
print("I'm the result!")
Material block:
```pythonexec="true" source="material-block"
print("I'm the result!")```
print("I'm the result!")
I'm the result!
Important:
The material-block source option requires that you enable the md_in_html Markdown extension.
Tabbed on the left:
```pythonexec="true" source="tabbed-left"
print("I'm the result!")```
print("I'm the result!")
I'm the result!
Important:
The tabbed-left source option requires that you enable the pymdownx.tabbed Markdown extension.
Tabbed on the right:
```pythonexec="true" source="tabbed-right"
print("I'm the result!")```
I'm the result!
print("I'm the result!")
Important:
The tabbed-left source option requires that you enable the pymdownx.tabbed Markdown extension.
Console (best used with actual session syntax like pycon or console):
```pyconexec="true" source="console"
>>> print("I'm the result!")I'm not the result...```
In the previous example, we didn't specify any title for tabs, so Markdown Exec used "Source" and "Result" by default. You can customize the titles with the tabs option:
```pythonexec="1" source="tabbed-left" tabs="Source code|Output"
print("I'm the result!")```
print("I'm the result!")
I'm the result!
As you can see, titles are separated with a pipe |. Both titles are stripped so you can add space around the pipe. If you need to use that character in a title, simply escape it with \|:
```pythonexec="1" source="tabbed-left" tabs="OR operator: a \|\| b | Boolean matrix"
print()print("a | b | a \\|\\| b")print("--- | --- | ---")forain(True,False):forbin(True,False):print(f"{a} | {b} | {aorb}")print()```
print()print("a | b | a \\|\\| b")print("--- | --- | ---")forain(True,False):forbin(True,False):print(f"{a} | {b} | {aorb}")print()
a
b
a || b
True
True
True
True
False
True
False
True
True
False
False
False
Important
The tabs option always expects the "Source" tab title first, and the "Result" tab title second. It allows to switch from tabbed-left to tabbed-right and inversely without having to switch the titles as well.
Limitation:
Changing the title for only one tab is not supported.
To change the console width for the execution of a code block, use the width option. Internally, Markdown Exec will set the COLUMNS environment variable accordingly, and restore its previous value after execution.
If the executed code doesn't support this environment variable, the default console width will be used (it could be the current width or some arbitrary value).
Code blocks execution can fail. For example, your Python code may raise exceptions, or your shell code may return a non-zero exit code (for shell commands that are expected to return non-zero, see Expecting a non-zero exit code).
In these cases, the exception and traceback (Python), or the current output (shell) will be rendered instead of the result, and a warning will be logged.
With many executed code blocks in your docs, it will be hard to know which code block failed exactly. To make it easier, you can set an ID on each code block with the id option, and this ID will be shown in the logs:
Markdown Exec makes it possible to persist state between executed code blocks. To persist state and reuse it in other code blocks, give a session name to your blocks:
Literate programming (LP) tools are used to obtain two representations from a source file: one understandable by a compiler or interpreter, the "tangled" code, and another for viewing as formatted documentation, which is said to be "woven" from the literate source.
We effectively support executing multiple nested code blocks to generate complex output. That makes for a very meta-markdown markup:
```mdexec="1" source="material-block" title="Markdown link"
[Link to example.com](https://example.com)
```
As seen in the Configuration section, Markdown Exec can be configured directly as a MkDocs plugin:
# mkdocs.ymlplugins:-search-markdown-exec
When configured this way, it will set a MKDOCS_CONFIG_DIR environment variable that you can use in your code snippets to compute file paths as relative to the MkDocs configuration file directory, instead of relative to the current working directory. This will make it possible to use the -f option of MkDocs, to build the documentation from a different directory than the repository root.
Example:
importosconfig_dir=os.environ['MKDOCS_CONFIG_DIR']# This will show my local path since I deploy docs from my machine:print(f"Configuration file directory: `{config_dir}`")