Skip to main content

Mermaid diagrams in yours Sphinx powered docs

Project description

This extension allows you to embed Mermaid graphs in your documents, including general flowcharts, sequence and gantt diagrams.

It adds a directive to embed mermaid markup. For example:

.. mermaid::

   sequenceDiagram
      participant Alice
      participant Bob
      Alice->John: Hello John, how are you?
      loop Healthcheck
          John->John: Fight against hypochondria
      end
      Note right of John: Rational thoughts <br/>prevail...
      John-->Alice: Great!
      John->Bob: How about you?
      Bob-->John: Jolly good!

By default, the HTML builder will simply render this as a div tag with class="mermaid", injecting the external javascript, css and initialization code to make mermaid works.

For other builders (or if mermaid_output_format config variable is set differently), the extension will use mermaid-cli to render as to a PNG or SVG image, and then used in the proper code.

You can also embed external mermaid files, by giving the file name as an argument to the directive and no additional content:

.. mermaid:: path/to/mermaid-gantt-code.mmd

As for all file references in Sphinx, if the filename is not absolute, it is taken as relative to the source directory.

In addition, you can use mermaid to automatically generate a diagram to show the class inheritance using the directive autoclasstree. It accepts one or more fully qualified names to a class or a module. In the case of a module, all the class found will be included.

Of course, these objects need to be importable to make its diagram.

If an optional attribute :full: is given, it will show the complete hierarchy of each class.

The option :namespace: <value> limits to the base classes that belongs to this namespace. Meanwhile, the flag :strict: only process the classes that are strictly defined in the given module (ignoring classes imported from other modules).

For example:

.. autoclasstree:: sphinx.util.SphinxParallelError sphinx.util.ExtensionError
   :full:

Or directly the module:

.. autoclasstree:: sphinx.util

Installation

You can install it using pip

pip install sphinxcontrib-mermaid

Then add sphinxcontrib.mermaid in extensions list of your project’s conf.py:

extensions = [
    ...,
    'sphinxcontrib.mermaid'
]

Directive options

:alt:: determines the image’s alternate text for HTML output. If not given, the alternate text defaults to the mermaid code.

:align:: determines the image’s position. Valid options are 'left', 'center', 'right'

:caption:: can be used to give a caption to the diagram.

Config values

mermaid_output_format

The output format for Mermaid when building HTML files. This must be either 'raw' 'png' or 'svg'; the default is 'raw'. mermaid-cli is required if it’s not raw

mermaid_version

The version of mermaid that will be used to parse raw output in HTML files. This should match a version available on https://unpkg.com/browse/mermaid/. The default is "latest".

If it’s set to "", the lib won’t be automatically included from the CDN service and you’ll need to add it as a local file in html_js_files. For instance, if you download the lib to _static/js/mermaid.js, in conf.py:

html_js_files = [
   'js/mermaid.js',
]

mermaid_init_js

Mermaid initilizaction code. Default to "<script>mermaid.initialize({startOnLoad:true});</script>". It will be included above the first diagram code.

mermaid_cmd

The command name with which to invoke mermaid-cli program. The default is 'mmdc'; you may need to set this to a full path if it’s not in the executable search path.

mermaid_cmd_shell

When set to true, the shell=True argument will be passed the process execution command. This allows commands other than binary executables to be executed on Windows. The default is false.

mermaid_params

For individual parameters, a list of parameters can be added. Refer to https://github.com/mermaidjs/mermaid.cli#options. Examples:

mermaid_params = ['--theme', 'forest', '--width', '600', '--backgroundColor', 'transparent']

This will render the mermaid diagram with theme forest, 600px width and transparent background.

mermaid_sequence_config

Allows overriding the sequence diagram configuration. It could be useful to increase the width between actors. It needs to be a json file Check options in the documentation

mermaid_verbose

Use the verbose mode when call mermaid-cli, and show its output in the building process.

mermaid_pdfcrop

If using latex output, it might be useful to crop the pdf just to the needed space. For this, pdfcrop can be used. State binary name to use this extra function.

Changelog

0.6.1 (January 31, 2021)

  • Fix a problem when called mermaid-cli

  • Fix typos on documentation

  • Improve internal code formatting (via black)

0.6.0 (January 31, 2021)

  • Drop support for Python version older than 3.6.

  • Allow to include javascript lib locally

  • Initilization code is now customizable

  • The default version included from the CDN is always the latest available.

0.5.0 (September 24, 2020)

  • Added mermaid_cmd_shell. Useful for Windows user.

  • Reimplement inheritance diagrams.

  • Fix UnicodeEncodeError on Python 2

0.4.0 (April 9, 2020)

  • Added mermaid_params

  • Added config file option

  • Improved latex integration

  • Added the pdfcrop functionality

  • Mermaid version is configurable

  • Several cleanups in the code

0.3.1 (Nov 22, 2017)

0.3 (Oct 4, 2017)

0.2.1 (Jun 4, 2017)

  • Workaround for opacity issue with rtd’s theme (thanks to Anton Koldaev)

0.2 (Jun 4, 2017)

  • Python 3 support fix (thanks to Shakeeb Alireza)

  • In-browser diagram generation

  • Autoclasstree directive. (Thanks to Zulko)

0.1.1 (Jun 4, 2017)

  • Better usage instructions

  • Bugfix

0.1 (Jul 18, 2016)

  • first public version

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

sphinxcontrib-mermaid-0.6.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinxcontrib_mermaid-0.6.1-py2.py3-none-any.whl (10.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sphinxcontrib-mermaid-0.6.1.tar.gz.

File metadata

  • Download URL: sphinxcontrib-mermaid-0.6.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.4

File hashes

Hashes for sphinxcontrib-mermaid-0.6.1.tar.gz
Algorithm Hash digest
SHA256 75ba8ab66b5157864360f5654a335af4a78384d5e9659c8d8e9ec9601d0a81b6
MD5 06008ed2a22468eeb4dba38d54992e8b
BLAKE2b-256 376173dc6c41049722d45bafac35b48d5e5de383ae8fd16fff15d447618c7fc1

See more details on using hashes here.

File details

Details for the file sphinxcontrib_mermaid-0.6.1-py2.py3-none-any.whl.

File metadata

  • Download URL: sphinxcontrib_mermaid-0.6.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.4

File hashes

Hashes for sphinxcontrib_mermaid-0.6.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b3914d0c7106da5cc275846711b023b9c2c07f82a823ef64281fe45370319e97
MD5 e28882c32bb642fccefeab50f70345c5
BLAKE2b-256 5334dc658d887f44c48ef276558cac3f8e41188e10aa78c02955e0526912d8a1

See more details on using hashes here.

Supported by

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