Skip to article frontmatterSkip to article content

Generate MyST markdown at execution time

You can use Jupyter cell execution to generate MyST Markdown at build time. This works because all notebook content is executed before being parsed as MyST Markdown.

To do so, follow a two-step process:

  1. Generate MyST Markdown in a temporary file at build time.
  2. Use the {include} directive to insert it into your content.

For example, the following code shows how you can generate MyST Markdown in a temporary file and insert it into the page.

Result
Code

Here’s the cell that is executed:

from pathlib import Path
text = """
:::{card} Here's a list of the files in this directory!
%s
:::
"""
source = Path().parent
files = "\n".join(f"- {ii}" for ii in source.rglob("*.md"))
build_folder = source / "../_build/tmp"
build_folder.mkdir(exist_ok=True, parents=True)
_ = (build_folder / "tmp.txt").write_text(text % files)

And here it is included: