Scroll to navigation



sphinxcontrib-mermaid - sphinxcontrib-mermaid documentation

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::

participant Alice
participant Bob
Alice->John: Hello John, how are you?
loop Healthcheck
John->John: Fight against hypochondria
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. [graph]

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



Or directly the module:

.. autoclasstree:: sphinx.util


You can install it using pip

pip install sphinxcontrib-mermaid

Then add sphinxcontrib.mermaid in extensions list of your project's

extensions = [

'sphinxcontrib.mermaid' ]


: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.



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


The version of mermaid that will be used to parse raw output in HTML files. This should match a version available on 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

html_js_files = [

'js/mermaid.js', ]


Mermaid initilizaction code. Default to "mermaid.initialize({startOnLoad:true});".

Changed in version 0.7: The init code doesn't include the <script> tag anymore. It's automatically added at build time.


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.


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.


For individual parameters, a list of parameters can be added. Refer to Examples:

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

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


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


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


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.


You can include Mermaid diagrams in your Markdown documents in Sphinx. You just need to setup the markdown support in Sphinx via myst-parser . See a minimal configuration from the tests

Then in your .md documents include a code block as in reStructuredTexts:


participant Alice
participant Bob
Alice->John: Hello John, how are you? ```

  • genindex
  • modindex
  • search


Martín Gaitán


2017-2022, Martín Gaitán

August 11, 2022