Skip to main content

mmdr

Render Mermaid diagrams from Python or the command line — fast, native, no browser required.

Most Mermaid rendering tools work by spinning up a headless browser and running the official JavaScript library. This works, but it's slow to start, heavy to install, and awkward to embed in a pipeline. mmdr takes a different approach: the rendering engine is written in Rust and ships as a compiled Python extension. Install it with pip, import it, and start rendering — no Node.js, no Puppeteer, no Chrome, no JS runtime of any kind.

pip install mmdr

That's it. No system dependencies, no extra steps.

Output formats: SVG · PNG · raw RGBA pixels · NumPy array

Two backends, one package:

  • merman (default) — targets full parity with the official Mermaid @11.15.0 library, supports all diagram types including newer ones like kanban, architecture, and c4
  • mermaid-rs-renderer — a lighter, faster alternative that covers the most common diagram types

CLI

mmdr ships with a command-line tool that works similarly to the official mermaid-cli (mmdc), but starts in milliseconds instead of seconds because there's no browser to boot.

Quick start

# render a file to SVG
mmdr -i diagram.mmd -o output.svg

# render to PNG
mmdr -i diagram.mmd -o output.png

# read from stdin, write to stdout — works great in pipelines
echo 'flowchart LR; A-->B-->C' | mmdr -i - -o -

# save the output to a file from a pipe
echo 'flowchart LR; A-->B-->C' | mmdr -i - -o diagram.svg

PNG options

mmdr -i diagram.mmd -o output.png \
  --width 1200 \               # canvas width in pixels
  --height 800 \               # canvas height in pixels
  --background '#ffffff'       # background color (transparent by default)

Backend and theme

# use the faster backend for simple diagrams
mmdr -i diagram.mmd -o output.svg --backend mermaid-rs-renderer

# classic Mermaid theme (mermaid-rs-renderer only)
mmdr -i diagram.mmd -o output.svg --backend mermaid-rs-renderer --theme classic

Batch rendering

# convert every .mmd file in a directory
for f in diagrams/*.mmd; do
    mmdr -i "$f" -o "${f%.mmd}.svg"
done

# same, but PNG with a white background
for f in diagrams/*.mmd; do
    mmdr -i "$f" -o "${f%.mmd}.png" --background '#ffffff'
done

Other commands

mmdr --info       # render Mermaid's built-in info diagram and show its text
mmdr --version    # print the mmdr version
mmdr -h           # full list of options

Python API

Rendering

mmdr.render() accepts a Mermaid source string and returns a Diagram object. The actual rendering is lazy — nothing runs until you ask for an output. The SVG result is cached, so calling .svg() multiple times only renders once.

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

svg = d.svg()   # str

# write to file
with open("output.svg", "w") as f:
    f.write(svg)

PNG

# default: transparent background, size determined by the diagram
png = d.png()

# with explicit size and background
png = d.png(width=1200, height=800, background="#ffffff")

with open("output.png", "wb") as f:
    f.write(png)

Save — auto-detect format

d.save("output.svg")                                  # writes SVG
d.save("output.png")                                  # writes PNG
d.save("output.png", width=1200, background="#ffffff")  # PNG with options

Raw pixels

.raw() returns the pixel buffer directly from the renderer as (bytes, width, height). The bytes are raw RGBA8888 data — 4 bytes per pixel, row-major, top-to-bottom. No encoding, no copying through an image library.

raw, w, h = d.raw(background="#ffffff")
print(f"image is {w}×{h} pixels, {len(raw)} bytes")

NumPy

.numpy() returns an (H, W, 4) array with dtype uint8 and RGBA channel order. It's built on .raw() — requires only numpy, no Pillow.

arr = d.numpy()
print(arr.shape)   # e.g. (480, 640, 4)
print(arr.dtype)   # uint8

# drop alpha → RGB
rgb = arr[:, :, :3]

# flip upside-down
import numpy as np
flipped = np.flipud(arr)

Jupyter / IPython

Diagram implements _repr_svg_(), so notebooks render it inline without any extra code:

import mmdr

# just evaluating this in a cell displays the diagram
mmdr.render("sequenceDiagram\n  Alice->>Bob: Hello!\n  Bob-->>Alice: Hi!")

Backends

mmdr.backends()
# ['merman', 'mermaid-rs-renderer']

# merman is the default — pick it explicitly if you want to be clear
d = mmdr.render("flowchart LR; A-->B", backend="merman")

# mermaid-rs-renderer for simpler diagrams where speed matters
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),
)

The theme, node_spacing, rank_spacing, and aspect_ratio options only apply to mermaid-rs-renderer. The merman backend uses its own layout engine tuned for parity with the official library.


Low-level utilities

If you already have an SVG string from another source, you can rasterize it directly without going through render():

from mmdr import svg_to_png, svg_to_raw

svg = open("diagram.svg").read()

png = svg_to_png(svg, width=1200, background="#ffffff")
raw, w, h = svg_to_raw(svg)

Supported diagram types

Diagram type merman mermaid-rs-renderer
flowchart / graph
sequenceDiagram
classDiagram
stateDiagram
erDiagram
pie
gantt
timeline
mindmap
gitGraph
xychart
block diagram
architecture
kanban
c4diagram
sankey
packet

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mmdr-0.2.0.tar.gz (38.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mmdr-0.2.0-cp39-abi3-win_amd64.whl (8.4 MB view details)

Uploaded CPython 3.9+Windows x86-64

mmdr-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

mmdr-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

mmdr-0.2.0-cp39-abi3-macosx_11_0_arm64.whl (7.3 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

mmdr-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file mmdr-0.2.0.tar.gz.

File metadata

  • Download URL: mmdr-0.2.0.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mmdr-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2c142d6d5c0694add0ea53a9668a14725402183a6a868e4b9b1a53082c4bde5b
MD5 1278220691d92262303305aaf1bb8adb
BLAKE2b-256 7b36699a01b2fbfbec162528af46f2760641479bffd7e5ec69add9df9baebf86

See more details on using hashes here.

File details

Details for the file mmdr-0.2.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: mmdr-0.2.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 8.4 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mmdr-0.2.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8424c12c71232cc6f2b5ee36f5bb0c96c408b647687abd4f01ca5be76c7b7530
MD5 998ed4dce7f0b0bb7e69a67f12569d75
BLAKE2b-256 3ea0adceec07c981b8799543253e81298b5dc6d34ff90a8757a1b5f8dae5f680

See more details on using hashes here.

File details

Details for the file mmdr-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mmdr-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b73ff1c25213c85a2eaeb44480e8d3de7994dd52c3f3b335f95b150447ed94da
MD5 6440a77c6b451b43bd00236f302f577c
BLAKE2b-256 e6af41a266a9e7c1b0e55ecb849569c5647e2db64b03345c185e4d5b50b1e3fe

See more details on using hashes here.

File details

Details for the file mmdr-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mmdr-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c79b8b4446d8b6891f4adcfb7bcaf6d50d9b75710357ba2ff9174117cd0eac4e
MD5 2e2522bc6958b398910b204598dd63c0
BLAKE2b-256 a2cd0f286f7a85414301f97da9c13b41406884abd97f5281a550828478e555dc

See more details on using hashes here.

File details

Details for the file mmdr-0.2.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: mmdr-0.2.0-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 7.3 MB
  • Tags: CPython 3.9+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mmdr-0.2.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 18d93cfe835a31ee8da8bbf92bd908f7ce4ff5571a1cb84a30786b90ac85b1c5
MD5 c51df412d067515869ec478e991a78f9
BLAKE2b-256 0ff2fc00e105e6e639e0d07b948148a08cd157c92f8e279af4a4507b1e4dca02

See more details on using hashes here.

File details

Details for the file mmdr-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for mmdr-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b0c6611933dabc7d275071e96f3f44a8e6b3ee275511d7bb8bb92ddcac77cf73
MD5 251e131562a166d685ce53cdf3886046
BLAKE2b-256 82798814ae6b9c7584827ba7cb02afc4825ce5ef6f40b2753af12c7ee53069d3

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.0

6 files

This release

0.2.0 This release

6 files

0.1.0

6 files

0.0.1

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page