Python API¶
mmdr.render()¶
Render a Mermaid diagram. Returns a Diagram object.
The SVG is rendered lazily — nothing runs until you call .svg(),
.png(), .save(), etc. The SVG result is cached, so calling .svg()
multiple times only renders once.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
diagram |
str |
— | Mermaid source text |
backend |
str \| None |
None ("merman") |
"merman" or "mermaid-rs-renderer" |
theme |
str |
"modern" |
"modern" or "classic" — mermaid-rs-renderer only |
node_spacing |
float |
— | Horizontal node spacing — mermaid-rs-renderer only |
rank_spacing |
float |
— | Vertical rank spacing — mermaid-rs-renderer only |
aspect_ratio |
tuple[float, float] |
— | Preferred aspect ratio — mermaid-rs-renderer only |
Example:
import mmdr
d = mmdr.render("flowchart LR; A-->B-->C")
d = mmdr.render("flowchart LR; A-->B-->C", backend="mermaid-rs-renderer")
d = mmdr.render(
"flowchart LR; A-->B-->C",
backend="mermaid-rs-renderer",
theme="classic",
node_spacing=60.0,
aspect_ratio=(16, 9),
)
Diagram¶
The object returned by mmdr.render().
.svg() → str¶
Return the diagram as an SVG string. Cached after first call.
.png(width, height, background) → bytes¶
Return the diagram as PNG bytes, rasterized via novasvg.
png = d.png()
png = d.png(width=1200, height=800, background="#ffffff")
with open("output.png", "wb") as f:
f.write(png)
| Parameter | Type | Default | Description |
|---|---|---|---|
width |
float \| None |
None |
Output width in pixels (height follows the aspect ratio if omitted) |
height |
float \| None |
None |
Output height in pixels |
background |
str \| None |
None |
Background fill as a CSS hex color or color name, e.g. "#ffffff" or "white". Transparent by default. |
.raw(width, height, background) → tuple[bytes, int, int]¶
Return raw RGBA8888 pixel data as (bytes, width, height).
Stride is width * 4, row-major, top-to-bottom, with straight
(non-premultiplied) alpha. No encoding — the pixel buffer comes straight out of novasvg.
raw, w, h = d.raw(background="#ffffff")
print(f"{w}×{h}, {len(raw)} bytes") # 640×480, 1228800 bytes
.numpy(width, height, background) → np.ndarray¶
Return an (H, W, 4) NumPy array, dtype uint8, RGBA channel order.
Requires NumPy (pip install mmdr[numpy]). No Pillow needed.
arr = d.numpy()
print(arr.shape) # (480, 640, 4)
print(arr.dtype) # uint8
# Drop alpha → RGB
rgb = arr[:, :, :3]
# Flip upside-down
import numpy as np
flipped = np.flipud(arr)
.save(output, width, height, background)¶
Save to a file. Format is inferred from the extension.
Supported extensions: .svg, .png.
.pdf() → bytes¶
Not yet implemented
PDF export is planned for a future release.
mmdr.backends() → list[str]¶
Return the list of backends compiled into this wheel.
Jupyter integration¶
Diagram implements _repr_svg_(), so it renders inline automatically
in Jupyter notebooks and IPython — no extra code needed: