markdown-include-variants: Markdown extension to expand directives to include source example files to also include their variants. Only useful to tiangolo's projets. Don't use it. 😅 · Pythie
Markdown extension to expand directives to include source example files to also include their variants. Only useful to tiangolo's projets. Don't use it. 😅
Actively maintained.Last release 2 weeks ago, commits in 13 of the last 26 weeks. A safe choice.
The last config is the one specific to this extension, markdown-include-variants.
The other configs are for the extensions that actually render the output, this extension (markdown-include-variants) just generates the content to be rendered by those other extensions.
Workflow
You add a Python source example to the docs_src directory, with the minimum Python version supported by the project, and using where applicable, named something like .
Annotated
tutorial001.py
If a non-Annotated alternative is useful, copy the file and name it with the suffix _non_an, like: tutorial001_non_an.py.
If an example needs alternatives for different Python versions, give every variant an explicit Python version suffix, like tutorial001_py311.py, tutorial001_py312.py, and tutorial001_non_an_py312.py.
In the Markdown file, include the preferred version of the file, using this syntax:
{* ./docs_src/first_steps/tutorial001.py *}
That will be automatically expanded with mdx-include to include the other variants below in a collapsed details box.
Variant Names
Filename
Display
tutorial001.py
Code block
tutorial001_non_an.py
non-Annotated
tutorial001_py312.py
Python 3.12+
tutorial001_non_an_py312.py
Python 3.12+ - non-Annotated
Examples without a version suffix target the project's supported Python versions. An explicit version suffix keeps its label even for a single file. Every variant of the same example must either have a Python version suffix or be unversioned.
Directory Variants
For examples spanning multiple files, use the same suffixes on directories, such as app and app_non_an:
The highlighted lines refer to the source file (for the included version), not the final rendered code block.
This makes it easier to decide which lines to highlight just by opening the source file, without having to calculate the actual lines in the rendered block after included, the extra lines for comments when omitting lines, etc.
Notice that the comment above adds 2 extra lines, and only the desired lines are included, the result is that the actual highlighted line is the rendered line 4, but the source line 6, it's all calculated automatically. 🤓
Include Only File Preview
If you only want to generate the "File Preview" collapsed details section, without actually including a tab of source files, you can use ln[0]:
{* ./docs_src/first_steps/tutorial001.py ln[0] *}
This is useful for example at the beginning of a new page, in a recap of the last used file in the previous page.
Migrating Existing Examples
Legacy _an names still work: tutorial001_an.py is recommended and its bare counterpart, tutorial001.py, is non-Annotated. This also applies to directory variants and pairs with matching Python versions.
Rename both sides of a pair together:
Existing filename
New filename
tutorial001_py310.py
tutorial001_non_an.py
tutorial001_an_py310.py
tutorial001.py
Keep explicit versions where alternatives still need them. Use _py38 for files previously relying on the implicit Python 3.8 version.
Update includes, test imports, configuration, and scripts to match. Each example family must use either the legacy _an convention or the new _non_an convention.
License
This project is licensed under the terms of the MIT license.