Skip to main content

markdown-include-variants

Test Coverage Package version

🚨 Warning: Internal Project

This is an internal project, it is mainly useful for the docs in @tiangolo's projects (e.g. all the FastAPI projects).

It is probably not useful to you. You should probably not use it. 😅

There are no guarantees about behavior, it is made to suit the needs of these projects, to simplify writing documentation.

How to Use

If you're still here it's probably because you are getting involved in one of the projects that use it. 🤓

Here's how it works. 🚀

Configure Plugin in MkDocs Material

Make sure mkdocs.yml has a section with at least these configs:

markdown_extensions:
  # Python Markdown Extensions
  pymdownx.highlight:
  pymdownx.superfences:

  # pymdownx blocks
  pymdownx.blocks.admonition:
    types:
    - tip
  pymdownx.blocks.details:
  pymdownx.blocks.tab:
    alternate_style: True

  # Other extensions
  mdx_include:
  markdown_include_variants:

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 Annotated where applicable, named something like 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:

{* ./docs_src/bigger_applications/app/routers/items.py title["app/routers/items.py"] *}

The extension finds the same relative file in each directory variant, including nested files. Use title to display a filename above the code block.

Include Lines

To include specific line ranges, use the config ln:

{* ./docs_src/first_steps/tutorial001.py ln[3:6,8,10:11] *}

That will include only:

  • lines 3 to 6
  • line 8
  • lines 10 to 11

It will add blocks with comments in the middle like:

# Code here omitted 👈️

If you include specific lines, it will also add another collapsed details box with the "Full file preview".

Highlight Lines

You can highlight specific lines using the config hl:

{* ./docs_src/first_steps/tutorial001.py hl[3,5:7,10] *}

That will highlight:

  • line 3
  • lines 5 to 7
  • line 10

Have in mind that the main file shown will be the one named in the include, and the highlights will apply to that file.

So, when deciding which lines to highlight, do that based on the file you include.

Include Lines and Highlight

You can combine both ln and hl:

{* ./docs_src/first_steps/tutorial001.py ln[3:6,8,10:11] hl[3,5:6,10] *}

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.

Include Lines and Highlight - Example

For example, you could have a source file with:

print("line 1")
print("line 2")
print("line 3")
print("line 4")
print("line 5")
print("line 6")  # highlight this
print("line 7")

Using a declaration like this:

{* ./docs_src/first_steps/tutorial001.py ln[5:7] hl[6] *}

Could render something like:

# Code above omitted 👈️

print("line 5")
print("line 6")  # highlight this
print("line 7")

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.

Metadata

Release files for markdown-include-variants 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markdown-include-variants 0.0.10
File Size Uploaded Provenance Actions
markdown_include_variants-0.0.10.tar.gz 63.0 kB With provenance Details for markdown_include_variants-0.0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for markdown-include-variants 0.0.10
File Interpreter ABI Platform Actions
markdown_include_variants-0.0.10-py3-none-any.whl Python 3 none any Details for markdown_include_variants-0.0.10-py3-none-any.whl

Total release size: 71.8 kB

Release files / markdown_include_variants-0.0.10.tar.gz

Download URL markdown_include_variants-0.0.10.tar.gz
Size 63.0 kB
Tags Source
SHA-256 checksum
How to use checksums
55b73051c2f825f85f274eebd3fecb0e099dd25ec12e488471ee349e734d0396
BLAKE2b-256 checksum
How to use checksums
abdc429e70f873f8460ea0d21405032dfb5322192a9376f539aa5cc0ccb7d9de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / markdown_include_variants-0.0.10-py3-none-any.whl

Download URL markdown_include_variants-0.0.10-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe889fd21ab85332814810108ae8b70ca1549818cfe349c3426025d9bc15855b
BLAKE2b-256 checksum
How to use checksums
5055bee38139f45ced70ce20d46cb18969602109bb14cb1c04c4749b3257dd2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Security for markdown-include-variants 0.0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for markdown_include_variants-0.0.10.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page