Graphviz is a Pelican plugin enables Graphviz images to be included using the Markdown or reStructuredText markup format. The Graphviz code for the image is included as a block in the article’s source. In the HTML output file, the Graphviz image can appear as either a <svg> element or embedded in an <img> element using the Base64 format.
This plugin can be installed via:
python -m pip install pelican-graphviz
The newly installed plugin should be detected and enabled automatically, unless the PLUGINS variable is used in the Pelican settings file. In this case, "graphviz" must be added to the existing PLUGINS list. Further information can be found in the How to Use Plugins documentation.
This plugin will be deactivated if Graphviz is not installed on the system. On Debian-based systems, Graphviz can be installed via:
sudo aptitude install graphviz
For macOS, Graphviz can be installed via Homebrew:
brew install graphviz
In the Markdown source, the Graphviz code should be inserted as a separate block, i.e. with blank lines separating it from the surrounding text. An example is shown below:
..graphviz dot
digraph G {
graph [rankdir = LR];
Hello -> World
}This will insert an image into your article, as shown here:
The block must start with ..graphviz (this is configurable — see below). The word dot in the first line indicates which program will be used to produce the image. The available programs are: dot, neato, twopi, circo, fdp, sfdp, and patchwork (see the Graphviz documentation for details). The Graphviz code must start on the second line of the block. Please note that newlines are not allowed inside the Graphviz block.
For RST input, support is implemented as a directive, with syntax like:
.. graphviz:: dot
:alt-text: My graph
digraph G {
graph [rankdir = LR];
Hello -> World
}The image is generated in HTML using an <img> element inside an HTML element (by default a <div>, but this can be configured — see below). The latter has the class graphviz (this is also configurable — see below). One possible CSS styling would be:
div.graphviz {
margin: 10px;
}
div.graphviz img {
display: block;
padding: 5px;
margin-left: auto;
margin-right: auto;
text-align: center;
border-style: solid;
border-width: 1px;
border-color: rgb(192, 177, 177);
-webkit-border-radius: 5px;
}The following variables can be configured in the Pelican settings file:
-
GRAPHVIZ_BLOCK_START: Starting tag for the Graphviz block in Markdown (defaults to'..graphviz'). This setting has no effect for reStructuredText input. -
GRAPHVIZ_HTML_ELEMENT: The HTML element inside which the generated Graphviz image is inserted (defaults to'div'; another sensible option would be'span'). -
GRAPHVIZ_IMAGE_CLASS: The class of the<div>element containing the generated Graphviz image (defaults to'graphviz'). -
GRAPHVIZ_COMPRESS: Compress the resulting SVG XML to an image (defaults toTrue). Without compression, more SVG features are available, for instance the inclusion of clickable URLs inside the Graphviz diagram. -
GRAPHVIZ_ALT_TEXT: The string that will be used as the default value for thealtproperty of the generated<img>HTML element (defaults to"[GRAPH]"). It is only meaningful when the resulting SVG output is compressed.
The values for all variables above, except GRAPHVIZ_BLOCK_START, can be overridden for each block individually using the following syntax in Markdown:
..graphviz [key1=val1, key2="val2"...] dotIf the value includes a comma (,) or an equal sign (=), then use the key2="val2" form.
Or in reStructuredText:
.. graphviz:: dot
:key1: val1
:key2: val2The allowed keys are html-element, image-class, alt-text, and compress. For the latter, the value can be either yes or no.
The embedded image is in SVG format, and cannot currently be changed. This format was chosen over others, such as PNG, for two reasons. First, the generated Base64 src string is usually shorter shorter for SVG than for PNG. Second, the image will be available in a high-quality vectorized format when displayed in the browser. However, note that this choice may prevent display in browsers lacking proper SVG support.
When generating compressed SVG images (the default), an <img> element will appear in the HTML output. According to the Web Content Accessibility Guidelines, non-text content, such as <img> elements, should have an text alternative. The graphviz plugin complies with this recommendation and always generate an alt property for the generated <img> element. The value of the alt property will be, in order of priority:
- The value of the Graphviz block option
alt-text, - the ID of the Graphviz element (
"G"in the example above), or - the value of the global configuration variable
GRAPHVIZ_ALT_TEXT.
An alternative to this plugin is the Graphviz tag provided by the Liquid Tags plugin, which differs from this plugin in several respects. First, the Liquid Tags version uses the syntax {% graphviz { <program> […] } %}, while the graphviz plugin uses the Markdown extension syntax ..graphviz <program> […]. The differences in the rendered output are:
- Both plugins output an element with the attribute
class="graphviz". However, only the graphviz plugin allows you to change the class name via a configuration variable (GRAPHVIZ_IMAGE_CLASS). - Liquid Tags encodes the image as
src="data:image/png;base64,[…]", whereas the graphviz plugin encodes it assrc="data:image/svg+xml;base64,[…]". This has two impacts. First, the size of the HTML code produced by the graphviz plugin is much smaller. For example, the Base64 string for the graphviz codedigraph graphname {a -> b -> c; b -> d;}is four times smaller. Second, the Liquid Tags version generates a raster image file, whereas the graphviz plugin produces a vector image that can be zoomed in on without losing image quality. - Liquid Tags outputs the Graphviz image inside a
<span>, whereas the graphviz plugin offers a choice for the container element.
Contributions are welcome and greatly appreciated. Every little bit helps. You can contribute by improving the documentation, adding missing features, and fixing bugs. You can also help out by reviewing and commenting on existing issues.
To start contributing to this plugin, review the Contributing to Pelican documentation, beginning with the Contributing Code section.
Thanks to Justin Mayer, for helping with migration of this plugin under the Pelican Plugins organization, and to Maxim Kochurov, for introducing the GRAPHVIZ_COMPRESS configuration variable.
Thanks to weeheavy for suggesting the addition of the alt property to the HTML <img> generated element (issue #30).
Thanks to Mark Shroyer for fixing lint issues, re-enabling the unit test TestGraphvizAltTextWithoutID (PR #31) and adding reStructuredText supoort (PRs #34 and #35)
Copyright © 2015, 2021, 2023–2025 Rafael Laboissière [email protected]
Copyright © 2025 Mark Shroyer [email protected]
This project is licensed under the terms of the AGPL license, version 3.0 or later.
