PlantUML Markdown Extension
plantuml-markdown is a Python-Markdown extension that enables embedding PlantUML diagrams directly into Markdown documents. It supports local PlantUML binary rendering, remote PlantUML servers, and Kroki servers. The current version is 3.11.1, with a regular release cadence addressing bug fixes and new features.
Warnings
- deprecated The `server` and `kroki_server` configuration options are deprecated starting from version 3.11.0.
- gotcha When using local PlantUML binary rendering, `plantuml-markdown` requires Java and Graphviz to be installed and the `plantuml` command to be available in your system's PATH. Without these, local rendering will fail.
- gotcha Prior to version 3.9.8, inline SVG output might have included an erroneous `ns0:` prefix on SVG tags, potentially causing rendering issues in some viewers.
- gotcha Include directives (`!include`) in PlantUML diagrams, especially when used with multiple `base_dir` paths, were subject to bugs in versions 3.10.0 through 3.10.4 regarding how included files were located.
- gotcha Version 3.11.0 contained a minor f-string formatting error that primarily affected Python versions older than 3.12, potentially leading to runtime issues.
Install
-
pip install plantuml-markdown
Imports
- plantuml_markdown
import markdown md = markdown.Markdown(extensions=['plantuml_markdown'], extension_configs={'plantuml_markdown': {'servers': ['http://www.plantuml.com/plantuml']}})
Quickstart
import markdown
import os
# For local rendering, ensure 'plantuml' command is in PATH and Java/Graphviz are installed.
# For remote rendering, use a server.
# This example uses a remote server for simplicity.
# In a real application, consider using os.environ for sensitive URLs or more robust configuration.
plantuml_server = os.environ.get('PLANTUML_SERVER_URL', 'http://www.plantuml.com/plantuml')
markdown_text = """
# My Document with a PlantUML Diagram
Here's a simple sequence diagram:
```plantuml format="svg"
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another action
```
And another one, using the alternative ::uml:: syntax:
::uml:: format="png"
Goofy -> MickeyMouse: calls
Goofy <-- MickeyMouse: responds
::end-uml::
"""
md = markdown.Markdown(extensions=['plantuml_markdown'], extension_configs={
'plantuml_markdown': {
'servers': [plantuml_server], # Using the new 'servers' config, introduced in 3.11.0
'cachedir': 'plantuml_cache' # Optional: enable caching
}
})
html_output = md.convert(markdown_text)
print(html_output)
# Example for MkDocs (mkdocs.yml)
# plugins:
# - plantuml_markdown:
# servers:
# - http://www.plantuml.com/plantuml
# cachedir: plantuml_cache