Skip to main content

mmdr

Render Mermaid diagrams to SVG, PNG or raw pixels from Python or the command line.
No browser, no Node.js, no Chromium — pip install mmdr is all you need.

PyPI Python CI License: MIT

Documentation · Quick start · Benchmarks · Changelog


import mmdr

d = mmdr.render("flowchart LR; A-->B-->C")
d.save("diagram.svg")
d.save("diagram.png", width=1200, background="#ffffff")

mmdr is a Python package with the Mermaid rendering engines written in Rust and compiled into the wheel. The usual way to render Mermaid outside a browser is mermaid-cli (mmdc), which starts headless Chrome for every diagram. mmdr skips that: there is no JavaScript runtime to boot, so it starts fast and drops easily into scripts, CI jobs and notebooks.

Highlights

  • Nothing to install besides the wheel. The Rust engines are inside it; the only Python dependency is novasvg, which pip fetches for you.
  • Two backends in one package. merman (default) aims for parity with the official Mermaid library; mermaid-rs-renderer is the faster one. Pick per call with backend=.
  • Much faster than mermaid-cli in the mermaidx benchmark. Median per diagram: 22.9 ms (merman) and 16.2 ms (mermaid-rs-renderer) against 791 ms for mmdc. Details and caveats below.
  • Four output formats. SVG, PNG, raw RGBA pixels and NumPy arrays. No Pillow.
  • A CLI that feels like mmdc. Files or stdin/stdout, so it works in pipelines.
  • Jupyter-ready. A Diagram displays itself inline.

Installation

pip install mmdr
Command Adds
pip install mmdr Both backends, the CLI, SVG / PNG / raw output.
pip install mmdr[numpy] NumPy, needed only by Diagram.numpy().

Quick start

Python

mmdr.render() returns a Diagram. Rendering is lazy: nothing runs until you ask for an output, and the SVG is cached after the first call.

import mmdr

d = mmdr.render("""
flowchart TD
    A[Start] --> B{Is it working?}
    B -- Yes --> C[Great!]
    B -- No  --> D[Debug it]
    D --> B
""")

svg = d.svg()                                   # str
png = d.png(width=1200, background="#ffffff")   # bytes, transparent if no background
raw, w, h = d.raw()                             # RGBA8888 bytes, width, height
arr = d.numpy()                                 # (H, W, 4) uint8 array, needs numpy
d.save("diagram.svg")                           # format follows the extension

In a notebook, evaluating mmdr.render("sequenceDiagram\n Alice->>Bob: Hello!") as the last line of a cell shows the diagram inline.

Command line

mmdr -i diagram.mmd -o diagram.svg
mmdr -i diagram.mmd -o diagram.png --width 1200 --background '#ffffff'
echo 'flowchart LR; A-->B-->C' | mmdr -i - -o diagram.svg
echo 'flowchart LR; A-->B-->C' | mmdr -i - -o - > diagram.svg

Convert every .mmd file in a folder:

for f in diagrams/*.mmd; do mmdr -i "$f" -o "${f%.mmd}.png" --background '#ffffff'; done

How it works

flowchart TB
    src["Mermaid text"] --> be{"backend"}
    be -->|"merman (default)"| svg["SVG string"]
    be -->|"mermaid-rs-renderer"| svg
    svg --> out1[".svg() / save to .svg"]
    svg --> nova["novasvg (Python)"]
    nova --> out2[".png() / save to .png"]
    nova --> out3[".raw() / .numpy()"]
Stage Done by Language
Parse, lay out, draw SVG merman or mermaid-rs-renderer Rust
SVG to PNG / pixels novasvg Python package

mmdr only renders Mermaid. To rasterize an SVG you already have, use novasvg directly:

import novasvg

doc = novasvg.Document.load_from_data(open("diagram.svg").read())
png = doc.render_to_bitmap(1200, -1, novasvg.Color("white")).to_png()

Backends

merman (default) mermaid-rs-renderer
Aim Parity with the official Mermaid library Fast rendering of common diagrams
Benchmark: diagrams drawn 50 of 50 45 of 50
Benchmark: median per diagram 22.9 ms 16.2 ms
Extra options none theme, node_spacing, rank_spacing, aspect_ratio
mmdr.backends()   # ['merman', 'mermaid-rs-renderer']

d = mmdr.render(
    "flowchart LR; A-->B-->C",
    backend="mermaid-rs-renderer",
    theme="classic",          # "modern" (default) or "classic"
    node_spacing=60.0,
    rank_spacing=80.0,
    aspect_ratio=(16, 9),
)

Invalid diagrams raise ValueError when the SVG is first needed.

Diagram support

Diagram type merman mermaid-rs-renderer Benchmark samples mermaid-rs-renderer drew
flowchart / graph ✅ ✅ 7 of 7
sequenceDiagram ✅ ⚠️ 2 of 5
classDiagram ✅ ⚠️ 1 of 2
stateDiagram ✅ ✅ 3 of 3
erDiagram ✅ ✅ 2 of 2
requirementDiagram ✅ ✅ 1 of 1
gitGraph ✅ ✅ 1 of 1
mindmap ✅ ✅ 2 of 2
timeline ✅ ✅ 2 of 2
journey ✅ ✅ 1 of 1
gantt ✅ ✅ 2 of 2
pie ✅ ✅ 2 of 2
quadrantChart ✅ ✅ 1 of 1
xychart ✅ ✅ 1 of 1
sankey ✅ ✅ 1 of 1
block ✅ ✅ 1 of 1
architecture ✅ ✅ 1 of 1
kanban ✅ ✅ 1 of 1
C4 ✅ ✅ 1 of 1
packet ✅ ✅ 1 of 1
radar ✅ ✅ 1 of 1
treemap ✅ ✅ 1 of 1
venn ✅ ✅ 1 of 1
ishikawa ✅ ✅ 1 of 1
wardley ✅ ✅ 1 of 1
ZenUML ✅ ✅ 3 of 3
ELK layout (flowchart-elk) ✅ ✅ 3 of 3
info ✅ ❌ 0 of 1

✅ drew every sample · ⚠️ rejected some samples · ❌ rejected the sample. The last column is from the mermaidx benchmark: 50 samples, merman drew all of them, mermaid-rs-renderer 45.

What mermaid-rs-renderer rejected, and raised ValueError for:

  • the bare info diagram;
  • three sequence diagrams: two use + / - activation shorthand (S->>+W), one uses a participant it could not resolve;
  • one class diagram with a <<abstract>> annotation.

A ✅ means an image came out, not that it is a correct one. We also ran one small diagram each of flowchart, sequence, class, state, ER, pie, gantt, timeline, mindmap, journey, gitGraph, xychart, quadrant, sankey, block, architecture, kanban, C4 and packet straight through mmdr 0.4.0, and both backends rendered all of them. Compare the pictures in the gallery before choosing mermaid-rs-renderer for a type.

The merman backend reports which Mermaid version it follows: run mmdr --info.

Benchmarks

The mermaidx benchmark renders the same 50 Mermaid diagrams with mermaid-cli and with each mermaidx backend. mermaidx uses mmdr as its engine for the rows below (rust:merman and rust:mermaid-rs-renderer).

Renderer Median per diagram Compared with mermaid-cli Produced an image First render in a process
mermaid-cli (mmdc) 791 ms reference 50 of 50 1.94 s
mmdr merman 22.9 ms 35× faster 50 of 50 7.1 ms
mmdr mermaid-rs-renderer 16.2 ms 41× faster 45 of 50 13.8 ms

How to read it:

  • The clock covers Mermaid text to PNG bytes. mmdc is started once per diagram, so its time includes starting Node.js and Chrome; that is the cost of using it in a script, not its layout speed.
  • Medians are taken over the diagrams each renderer handled. The speed-up is the median of per-diagram ratios.
  • Producing an image does not mean producing a correct one. Compare the pictures in the gallery before choosing a backend.
  • Absolute times depend on the CI machine that ran the job. See how the benchmarks work.

API and CLI essentials

Python Returns Notes
mmdr.render(source, backend=None, **opts) Diagram Lazy. backend is "merman" (default) or "mermaid-rs-renderer".
Diagram.svg() str Cached after the first call.
Diagram.png(width, height, background) bytes Transparent unless background is given. If only one size is given, the other follows the aspect ratio.
Diagram.raw(width, height, background) (bytes, w, h) Straight (non-premultiplied) RGBA8888.
Diagram.numpy(width, height, background) ndarray (H, W, 4), uint8. Needs mmdr[numpy].
Diagram.save(path, ...) None .svg or .png; .pdf is not supported yet.
mmdr.backends() list[str] Backends compiled into this wheel.
CLI flag Meaning
-i, --input Input file; - (default) reads stdin.
-o, --output Output file; - writes to stdout. The format follows the extension.
-e, --format Force svg or png.
--backend merman (default) or mermaid-rs-renderer.
-w, --width / -H, --height PNG size in pixels.
-b, --background PNG background, e.g. '#ffffff' or white.
-t, --theme, --node-spacing, --rank-spacing, --aspect-ratio W:H mermaid-rs-renderer only.
--info, --version, -h Show Mermaid's built-in info diagram, the mmdr version, or all options.

Full reference: documentation.

Requirements

  • Python 3.9 or newer, on Windows, macOS or Linux (pre-built wheels).
  • To build from source: Rust (the merman crate needs Rust 1.95 or newer) and maturin.

Testing

pip install maturin pytest numpy
maturin develop --release
pytest tests/

Contributing

Issues and pull requests are welcome at github.com/MohammadRaziei/mmdr.

License

MIT. See LICENSE.

Acknowledgements

mmdr wraps two Rust Mermaid renderers, mermaid-rs-renderer by 1jehuang and merman, and rasterizes with novasvg.

Metadata

Release files for mmdr 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mmdr 0.4.1
File Size Uploaded Provenance Actions
mmdr-0.4.1.tar.gz 45.0 kB Without provenance Details for mmdr-0.4.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for mmdr 0.4.1
File Actions
mmdr-0.4.1-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for mmdr-0.4.1-cp39-abi3-win_amd64.whl
mmdr-0.4.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for mmdr-0.4.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
mmdr-0.4.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for mmdr-0.4.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
mmdr-0.4.1-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for mmdr-0.4.1-cp39-abi3-macosx_11_0_arm64.whl
mmdr-0.4.1-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for mmdr-0.4.1-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 49.9 MB

Release files / mmdr-0.4.1.tar.gz

Download URL mmdr-0.4.1.tar.gz
Size 45.0 kB
Tags Source
SHA-256 checksum
How to use checksums
23645c3138df6b3a0a5be02775097d0c1de938e62c3aa3f99a116e6e9935f04f
BLAKE2b-256 checksum
How to use checksums
104550e7f2d325884254f837ecc9f2761709e9940d2be16ec3db41e1caba6ea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / mmdr-0.4.1-cp39-abi3-win_amd64.whl

Download URL mmdr-0.4.1-cp39-abi3-win_amd64.whl
Size 10.2 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
1426f5ea3e6e117335e70bb558fb58286630c04c3a0dcfccf360356edaaa41e7
BLAKE2b-256 checksum
How to use checksums
9cb14aa72fc398047d548603da3bef0e044a6fa49d854c32fc7321f360eb29ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / mmdr-0.4.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL mmdr-0.4.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 10.7 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
f72ac0778b91d7151b475241e88ce2503ee3925785da1ec2f9e906df984fb5a1
BLAKE2b-256 checksum
How to use checksums
61164f618542dae315d52a77163c04ca95e7b0a60b9231b884922529157e9925
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / mmdr-0.4.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL mmdr-0.4.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 9.7 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
b5a258e24bfe897cc6dd2d818aca53b6e006833b7b1bf4fb33fbc58cd651acaa
BLAKE2b-256 checksum
How to use checksums
51b36de1b0bffe5a02928448e7264eb7fdccf859e0729e4cccdeb3c843300fbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / mmdr-0.4.1-cp39-abi3-macosx_11_0_arm64.whl

Download URL mmdr-0.4.1-cp39-abi3-macosx_11_0_arm64.whl
Size 9.2 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
421ca62ce4e9401b9d49b29dad01b802b414f5cc9f1d66fb80153e748487f906
BLAKE2b-256 checksum
How to use checksums
41a34bc762053649492ef6ea93409fc991644824f189723c21d65b20cfb6bf25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / mmdr-0.4.1-cp39-abi3-macosx_10_12_x86_64.whl

Download URL mmdr-0.4.1-cp39-abi3-macosx_10_12_x86_64.whl
Size 10.2 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
be8d73dc083ccd1c6ee5a8727c6063ab02ba3cb8c9a73c64b7a9853b0ef7d059
BLAKE2b-256 checksum
How to use checksums
97662134c5ef5e8f066c0d6b1e9e676985fc3b7f0ee251cc8625d6ed8fa4b98d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for mmdr 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page