sphinx_code_tabs¶
This is a Sphinx extension that adds a directive code-tabs
that creates a
navbar above several alternative code blocks, allowing the user to switch
between them.
This can be used to e.g. show a snippet in multiple languages, display instructions for alternative platforms, or (in the future) switch between source and output.
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 code-tabs
directive that
declares a notebook of code block alternatives which looks as follows:
echo "Hello, World!"
#include <stdio.h>
int main() { printf("Hello, world!\n"); }
print("Hello, world!")
The individual tabs must be created with the code-tab
directive which
derives from code-block
and accepts all of its arguments.
For example, this is the source of above example:
.. code-tabs::
.. code-tab:: bash
:title: bash
echo "Hello, World!"
.. code-tab:: c
:title: C/C++
:emphasize-lines: 2
#include <stdio.h>
int main() { printf("Hello, world!\n"); }
.. code-tab:: python
:title: python
print("Hello, world!")