Markdown Inline Graphviz Extension
The `markdown-inline-graphviz-extension` library is a Python Markdown extension that allows rendering of Graphviz DOT language diagrams directly within Markdown documents, replacing inline definitions with inline SVG or PNG images. It is a Python 3 fork and continuation of previous projects, designed to work seamlessly with any Python-Markdown-based static site generator. Its current version is 1.1.3, with updates occurring on an as-needed basis to ensure compatibility and address issues.
Common errors
-
ERROR - Config value: 'markdown_extensions'. Error: Failed loading extension "inline_graphviz". Aborted with 1 Configuration Errors!
cause The extension name provided in the configuration (e.g., `mkdocs.yml` or the `extensions` list) is incorrect or misspelled.fixCorrect the extension name to `markdown_inline_graphviz`. For MkDocs, this would be in `mkdocs.yml` under `markdown_extensions: - markdown_inline_graphviz`. -
Error: Failed to execute dot. Verify that Graphviz is installed and in your system's PATH. (or similar errors like 'dot not found', 'neato not found')
cause The Graphviz system executables (like `dot`, `neato`, etc.) are not installed on your operating system or are not accessible via your system's PATH environment variable. The Python `graphviz` library, which `markdown-inline-graphviz-extension` uses, relies on these external tools.fixInstall Graphviz on your system. For Debian/Ubuntu: `sudo apt-get install graphviz`. For macOS: `brew install graphviz`. For Windows, download from graphviz.org. Ensure the installation directory is added to your system's PATH. -
Graphs are not rendered, or outdated graphs are displayed, even after fixing the DOT code.
cause Graphviz errors within the DOT code might be reported only once by the underlying `graphviz` tool, and subsequent builds might reuse old cached images or fail silently. This can lead to a misleading state where changes aren't reflected or errors are hard to debug.fixIf a graph fails to render, carefully check your DOT language syntax. Clear any build caches or temporary output directories that the Markdown processor might use. If manually debugging, run the `dot` command with your graph definition directly in the terminal to identify syntax errors before integrating with Markdown.
Warnings
- gotcha The underlying `graphviz` Python package (a dependency) requires the Graphviz `dot` executable and other layout engines (e.g., `neato`, `fdp`) to be installed on your system and available in the system's PATH. Without these system executables, the extension will fail to render graphs, potentially with errors from the `graphviz` Python library or simply outputting empty placeholders.
- gotcha The correct extension string to use with `markdown.markdown()` is `'markdown_inline_graphviz'`. Incorrect spelling or capitalization (e.g., `inline_graphviz`, `Markdown_Inline_Graphviz`, `markdown_graphviz_inline`) will prevent the extension from loading and processing graph blocks, leading to unrendered graph definitions in your output HTML.
- breaking This `markdown-inline-graphviz-extension` library is a Python 3 fork and continuation of the original `markdown-inline-graphviz` (by Steffen Prince, `sprin/markdown-inline-graphviz`). While core functionality is similar, users migrating from the older Python 2-compatible libraries should be aware of potential subtle behavioral differences or changes in required `markdown` library versions. The library explicitly targets Python 3 environments.
Install
-
pip install markdown-inline-graphviz-extension
Imports
- Extension String
from markdown_inline_graphviz_extension import InlineGraphvizExtension
import markdown html = markdown.markdown(text, extensions=['markdown_inline_graphviz'])
Quickstart
import markdown
import os
# Ensure Graphviz 'dot' executable is in your system's PATH
# For example, on Ubuntu/Debian: sudo apt-get install graphviz
# On macOS: brew install graphviz
markdown_text = """
# My Document with a Graph
Here is a simple graph:
{% dot example.svg
digraph G {
A -> B;
B -> C;
C -> A;
}
%}
Another graph using a different engine:
{% neato another.svg
node [shape=box];
A -- B;
B -- C;
C -- A;
%}
"""
# Render the markdown with the inline_graphviz extension
# Ensure the extension name is exactly 'markdown_inline_graphviz'
try:
html_output = markdown.markdown(markdown_text, extensions=['markdown_inline_graphviz'])
print(html_output)
except Exception as e:
print(f"Error rendering markdown: {e}")
print("Please ensure Graphviz is installed on your system and its executables are in your PATH.")
print("You can test by running 'dot -V' or 'neato -V' in your terminal.")