Code Monkey home page Code Monkey logo

sphinx-code-tabs's Introduction

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:

sphinx-code-tabs's People

Contributors

coldfix avatar dependabot[bot] avatar pkulev avatar helveg avatar sigeryang avatar

Stargazers

V avatar kareobana avatar  avatar Ujjwal Panda avatar Philip Mocz avatar  avatar  avatar ae avatar greenrock avatar  avatar  avatar Amaury Chamayou avatar Roy Kaldung avatar  avatar Philipp Glira avatar Nikolaos Dionelis avatar Sergey Vasilyev avatar Kaken Bok avatar  avatar André-Patrick Bubel avatar

Watchers

James Cloos avatar  avatar Nikolaos Dionelis avatar  avatar

sphinx-code-tabs's Issues

Giving `code-tab` an unknown lexer gives cryptic error

Building docs with sphinx 4.3.0 and this code

.. code-tabs::

  .. code-tab:: pythonmm

    print("Hello world")

yields

  File "/home/robin/.pyenv/versions/3.9.7/lib/python3.9/site-packages/docutils/writers/_html_base.py", line 379, in starttag
    assert value is not None
AssertionError

If a :title: is given to the offending code-tab the error is less severe and gives a good message:

/home/robin/git/patch/docs/source/sections.rst:112: WARNING: Pygments lexer name 'pythonmm' is not known

Building PDF not supported

Thank you for the nice looking tabs. I got troubles building the docs when PDF option is enabled:

image

https://readthedocs.org/projects/afeefa-api-resources/builds/13149454/

processing afeefaapi-resources.tex... index development relations

Exception occurred:
File "/home/docs/checkouts/readthedocs.org/user_builds/afeefa-api-resources/envs/latest/lib/python3.7/site-packages/sphinx/writers/latex.py", line 2041, in unknown_visit
raise NotImplementedError('Unknown node: ' + node.class.name)
NotImplementedError: Unknown node: Tab
The full traceback has been saved in /tmp/sphinx-err-536htcsw.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at https://github.com/sphinx-doc/sphinx/issues. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.