Skip to main content

sphinx code tabs

Latest Version License: Unlicense Documentation

This is a Sphinx extension that adds a tabs directive for creating a tabbed widget, allowing the user to switch between them. The individual tabs can be code blocks or general content.

This can be used to e.g. show a snippet in multiple languages, display instructions for alternative platforms, or switch between code and output.

Code tabs screenshot

Installation

pip install sphinx_code_tabs

To enable the extension in sphinx, simply add the package name in your conf.py to the list of extensions:

extensions = [
    ...
    'sphinx_code_tabs',
]

Usage

By enabling the extension you get access to the tabs directive that declares a notebook of code block alternatives.

The individual tabs are created with the tab or code-tab directives. A tab can contain arbitrary restructuredText, while a code-tab acts like a code-block and accepts all corresponding arguments. Both types of tabs can appear in the same notebook.

The :selected: option allows to switch to a specified tab at start. By default, the first tab is used.

For example, this is the source of above example:

.. tabs::

    .. code-tab:: bash

        echo "Hello, *World*!"

    .. code-tab:: c
        :caption: C/C++
        :emphasize-lines: 2

        #include <stdio.h>
        int main() { printf("Hello, *World*!\n"); }

    .. code-tab:: python

        print("Hello, *World*!")

    .. tab:: Output
        :selected:

        Hello, *World*!

Grouped tabs

The tabs directive takes an optional argument that identifies its tab group. Within a given tab group, all notebooks will automatically be switched to the same tab number if the tab is switched in one member of the group. It is your responsibility to make sure that each member of the group has the same number and ordering of tabs. Example:

Grouped tabs screenshot

Source:

.. tabs:: lang

    .. code-tab:: bash

        echo "Hello, group!"

    .. code-tab:: python

        print("Hello, group!")


.. tabs:: lang

    .. code-tab:: bash

        echo "Goodbye, group!"

    .. code-tab:: python

        print("Goodbye, group!")

Alternatives

After creating this package, I found other packages which are functionally similar or equivalent. You may want to check them out if sphinx-code-tabs doesn’t fit your needs:

CHANGES

v0.5.4

Date: 13.07.2023

  • no changes, release needed to trigger a new build

v0.5.4

Date: 28.06.2023

  • declare extension as safe for parallel reads (#6)

v0.5.3

Date: 28.11.2021

  • fix CHANGES to appear in long description

v0.5.2

Date: 28.11.2021

  • fix ImportError triggered on readthedocs due to ancient sphinx version (v1.8)

v0.5.1

Date: 28.11.2021

  • update description for landing page

v0.5.0

Date: 28.11.2021

  • add tab directive for arbitrary (non-code) content

  • add tabs directive and make code-tabs a backward-compatibility alias of tabs, to account for the new more general tab containers

  • make the :title: option no longer required (wasn’t enforced anyway by sphinx)

  • add grouped tabs

  • make non-code tabs look better in latex output by boxing them like listings

v0.4.0

Date: 27.11.2021

  • fix bug that selects all tab buttons on click (introduced in prerender commit)

v0.3.0

Date: 27.11.2021

  • fix AssertionError if :title: option is missing (see #3)

  • increase css specificity to fix big margins that have appeared due to some CSS change in sphinx or rtd

  • fix exception when building pdf documents (#1, #4)

  • prerender tab hidden/selected state to avoid content reflow on page (re-)load

v0.2.0

Date: 21.06.2021

  • update css for sphinx 4

v0.1.0

Date: 10.10.2020

  • fix missing assets when using the extension on readthedocs

  • add documentation along with visual example on readthedocs

v0.0.1

Date: 10.10.2020

Initial version with basic functionality:

  • all rendering is done by JS, no prerendering

  • so far no “notebook-groups” that switch the language simultaneously

Metadata

Release files for sphinx-code-tabs 0.5.5

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

Source distribution (sdist)

Source distribution for sphinx-code-tabs 0.5.5
File Size Uploaded Provenance
sphinx_code_tabs-0.5.5.tar.gz 9.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sphinx-code-tabs 0.5.5
File Interpreter ABI Platform
sphinx_code_tabs-0.5.5-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / sphinx_code_tabs-0.5.5.tar.gz

Download URL sphinx_code_tabs-0.5.5.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
150acc5190ce3b3d2378af02dc3fb651d32cbfb924a5ed37681418aa7520acea
BLAKE2b-256 checksum
How to use checksums
46b0c70a7de8d27d8bfd4cc5ca799f84e46c963fde48269572bd681a9980c348
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / sphinx_code_tabs-0.5.5-py3-none-any.whl

Download URL sphinx_code_tabs-0.5.5-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2cabdcc02b5897b399f6bca36557ce050d2fb7d095c01292b41ad34f1ce2ef0
BLAKE2b-256 checksum
How to use checksums
fe815508858d971e5170ef4279de6cc805df3206737bfd86f697aa4a9e1e88e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for sphinx-code-tabs 0.5.5

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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