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:
- Generate MyST Markdown in a temporary file at build time.
- 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:
Here's the cell that is executed:
````{code-cell} python
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:
```{include} ../_build/tmp/tmp.txt
```