Skip to main content

Automatically generate code examples for different Python versions in mkdocs or Sphinx based documentations

Project description

AutoPyTabs

Automatically generate code examples for different Python versions in mkdocs or Sphinx based documentations, or a plain markdown workflow, making use of the pymdown "tabbed" markdown extension for markdown, and sphinx{design} tabs for Sphinx.

Rationale

The problem

Python project documentation typically include code examples. Given that most of the time, a project will support multiple versions of Python, it would be ideal to showcase the adjustments that can or need to be made for different Python versions. This can be achieved by including several versions of the example code, conveniently displayed using the pymdown "tabbed" extension for markdown, or sphinx{design} tabs for Sphinx.

This, however, raises several problems:

  1. Maintaining multiple versions of a single example is tedious and error-prone as they can easily become out of sync
  2. Figuring out which examples need to be changed for which specific Python version is a labour intensive task
  3. Dropping or adding support for Python versions requires revisiting every example in the documentation
  4. Checking potentially ~4 versions of a single example into VCS creates unnecessary noise

Given those, it's no surprise that the current standard is to only show examples for the lowest supported version of Python.

The solution

AutoPyTabs aims to solve all of these problems by automatically generating versions (using the awsome ruff project) of code examples, targeting different Python versions at build-time, based on a base version (the lowest supported Python version). This means that:

  1. There exists only one version of each example: The lowest supported version becomes the source of truth, therefore preventing out-of-sync examples and reducing maintenance burden
  2. Dropping or adding support for Python versions can be done via a simple change in a configuration file

Table of contents

  1. Usage with mkdocs / markdown
    1. Configuration
    2. Differences between the mkdocs plugin vs markdown extension
    3. Examples
    4. Selectively disable
    5. Compatibility with pymdownx.snippets
  2. Usage with Sphinx
    1. Configuration
    2. Directives
    3. Examples
    4. Compatibility with other extensions

Installation

For mkdocs: pip install auto-pytabs[mkdocs] For markdown: pip install auto-pytabs[markdown] For sphinx: pip install auto-pytabs[sphinx]

Usage with mkdocs / markdown

Configuration

Mkdocs plugin

site_name: My Docs
markdown_extensions:
  - pymdownx.tabbed:
plugins:
  - auto_pytabs:
      min_version: "3.7"  # optional
      max_version: "3.11" # optional
      tab_title_template: "Python {min_version}+"  # optional
      no_cache: false  # optional
      default_tab: "highest"  # optional
      reverse_order: false  # optional

Available configuration options

Name Default Description
min_version (3, 7) Minimum python version
max_version (3, 7) Maximum python version
tab_title_template "Python {min_version}+" Template for tab titles
no_cache False Disable file system caching
default_tab highest (highest or lowest) Version tab to preselect
reverse_order False Reverse the order of tabs. Default is to go from lowest to highest version

Markdown extension

import markdown

md = markdown.Markdown(
    extensions=["auto_pytabs"],
    extension_configs={
        "auto_pytabs": {
            "min_version": "3.7",  # optional
            "max_version": "3.11",  # optional
            "tab_title_template": "Python {min_version}+",  # optional
            "default_tab": "highest",  # optional
            "reverse_order": False,  # optional
        }
    },
)

Available configuration options

Name Default Description
min_version (3, 7) Minimum python version to generate code for
max_version (3, 7) Maximum python version to generate code for
tab_title_template "Python {min_version}+" Template for tab titles
default_tab highest (highest or lowest) Version tab to preselect
reverse_order False Reverse the order of tabs. Default is to go from lowest to highest version

Differences between the mkdocs plugin and markdown extension

AutoPyTabs ships as a markdown extension and an mkdocs plugin, both of which can be used in mkdocs. The only difference between them is that the mkdocs plugin supports caching, which can make subsequent builds faster (i.e. when using mkdocs serve). The reason why the markdown extension does not support caching is that markdown does not have clearly defined build steps with wich an extension could interact (like mkdocs plugin events), making it impossible to know when to persist cached items to disk / evict unused items.

If you are using mkdocs, the mkdocs plugin is recommended. If you have caching disabled, there will be no difference either way.

Should you wish to integrate the markdown extension into a build process where you can manually persist the cache after the build, you can explicitly pass it a cache:

import markdown
from auto_pytabs.core import Cache

cache = Cache()

md = markdown.Markdown(
    extensions=["auto_pytabs"],
    extension_configs={
        "auto_pytabs": {
           "cache": cache
        }
    },
)


def build_markdown() -> None:
    md.convertFile("document.md", "document.html")
    cache.persist()

Examples

Input

```python
from typing import Optional, Dict

def foo(bar: Optional[str]) -> Dict[str, str]:
    ...
```

Equivalent markdown

=== "Python 3.7+"
    ```python
    from typing import Optional, Dict

    def foo(bar: Optional[str]) -> Dict[str, str]:
        ...
    ```

=== "Python 3.9+"
    ```python
    from typing import Optional
    
    
    def foo(bar: Optional[str]) -> dict[str, str]:
        ...
    ```

==== "Python 3.10+"
    ```python
    def foo(bar: str | None) -> dict[str, str]:
        ...
    ```

Nested blocks

Nested tabs are supported as well:

Input

=== "Level 1-1"

    === "Level 2-1"

        ```python
        from typing import List
        x: List[str]
        ```

    === "Level 2-2"
    
        Hello, world!

=== "Level 1-2"

    Goodbye, world!

Equivalent markdown

=== "Level 1-1"

    === "Level 2-1"

        === "Python 3.7+"
            ```python
            from typing import List
            x: List[str]
            ```
        
        === "Python 3.9+"
            ```python
            x: list[str]
            ```

    === "Level 2-2"

        Goodbye, world!

=== "Level 1-2"
    Hello, world!
    

Selectively disable

You can disable conversion for a single code block:

<!-- autopytabs: disable-block -->
```python
from typing import Set, Optional

def bar(baz: Optional[str]) -> Set[str]:
    ...
```

Or for whole sections / files

<!-- autopytabs: disable -->
everything after this will be ignored
<!-- autopytabs: enable -->
re-enables conversion again

Compatibility with pymdownx.snippets

If the pymdownx.snippets extension is used, make sure that it runs before AutoPyTab


Usage with Sphinx

AutPyTabs provides a Sphinx extension auto_pytabs.sphinx_ext, enabling its functionality for the .. code-block and .. literalinclude directives.

Configuration

Example configuration

extensions = ["auto_pytabs.sphinx_ext", "sphinx_design"]

auto_pytabs_min_version = (3, 7)  # optional
auto_pytabs_max_version = (3, 11)  # optional
auto_pytabs_tab_title_template = "Python {min_version}+"  # optional 
# auto_pytabs_no_cache = True  # disabled file system caching
# auto_pytabs_compat_mode = True  # enable compatibility mode
# auto_pytabs_default_tab = "lowest"  # Pre-select the tab with the lowest version
# auto_pytabs_reverse_order = True  # reverse the order of tabs to highest > lowest

Available configuration options

Name Default Description
auto_pytabs_min_version (3, 7) Minimum python version to generate code for
auto_pytabs_max_version (3, 7) Maximum python version to generate code for
auto_pytabs_tab_title_template "Python {min_version}+" Template for tab titles
auto_pytabs_no_cache False Disable file system caching
auto_pytabs_compat_mode False Enable compatibility mode
auto_pytabs_default_tab highest Either highest or lowest. Version tab to preselect
auto_pytabs_reverse_order False Reverse the order of tabs. Default is to go from lowest to highest version

Examples

Input

.. code-block:: python

   from typing import Optional, Dict
   
   def foo(bar: Optional[str]) -> Dict[str, str]:
       ...

Equivalent ReST

.. tab-set::

   .. tab-item:: Python 3.7+
   
       .. code-block:: python
       
          from typing import Optional, Dict
      
          def foo(bar: Optional[str]) -> Dict[str, str]:
              ...

   .. tab-item:: Python 3.9+
   
      .. code-block:: python
      
          from typing import Optional
          
          
          def foo(bar: Optional[str]) -> dict[str, str]:
              ...

   .. tab-item:: Python 3.10+
   
      .. code-block:: python
      
          def foo(bar: str | None) -> dict[str, str]:
              ...

Directives

AutoPyTabs overrides the built-in code-block and literal-include directives, extending them with auto-upgrade and tabbing functionality, which means no special directives, and therefore changes to existing documents are needed.

Additionally, a :no-upgrade: option is added to the directives, which can be used to selectively fall back the default behaviour.

Two new directives are provided as well:

  • .. pytabs-code-block::
  • .. pytabs-literalinclude::

which by default act exactly like .. code-block and .. literalinclude respectively, and are mainly to provide AutoPyTab's functionality in compatibility mode.

Compatibility mode

If you don't want the default behaviour of directive overrides, and instead wish to use the .. pytabs- directives manually (e.g. because of compatibility issues with other extensions or because you only want to apply it to select code blocks) you can make use AutoPyTabs' compatibility mode. To enable it, simply use the auto_pytabs_compat_mode = True in conf.py. Now, only content within .. pytabs- directives will be upgraded.

Compatibility with other extensions

Normally the directive overrides don't cause any problems and are very convenient, since no changes to existing documents have to be made. However, if other extensions are included, which themselves override one of those directives, one of them will inadvertently override the other, depending on the order they're defined in extensions.

To combat this, you can use the compatibility mode extension instead, which only includes the new directives.

If you control the conflicting overrides, you can alternatively inherit from auto_py_tabs.sphinx_ext.CodeBlockOverride and auto_py_tabs.sphinx_ext.LiteralIncludeOverride instead of sphinx.directives.code.CodeBlock and sphinx.directives.code.LiteralInclude respectively.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_pytabs-0.4.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

auto_pytabs-0.4.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file auto_pytabs-0.4.0.tar.gz.

File metadata

  • Download URL: auto_pytabs-0.4.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1036-azure

File hashes

Hashes for auto_pytabs-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4c596aa02ea20c6c85809e5f60a22aa60499dcaa637e52d6313d07c58c5bb61e
MD5 cf6423db35ef18dfdcc10a1bd27728a8
BLAKE2b-256 c39023e4632abb5684463c9f4d94493f3319ca718be4965368ec8606de35d5ab

See more details on using hashes here.

File details

Details for the file auto_pytabs-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: auto_pytabs-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1036-azure

File hashes

Hashes for auto_pytabs-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 941ca4f21b218249ee4d026ebaf4a8a7788a066fdb223571f1f7b93d44ac6a74
MD5 664b6a8de9fe28b44c88fc4b31e5b5ca
BLAKE2b-256 5aca93b4f045cab0dce2b3f6252464b7ae2ac2f5d71b0757174440e577226046

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page