mermaid-py

0.8.4 · active · verified Fri Apr 17

`mermaid-py` is a Python interface for the popular `mermaid-js` library, simplifying the creation and rendering of diagrams directly from Python code. It currently targets the latest `Mermaid.js v10+` and provides methods to output diagrams as HTML, JSON, or images. The current version is 0.8.4, with releases typically following major Mermaid.js updates or API improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Mermaid diagram string, create a `Mermaid` object, and retrieve its HTML and JSON representations. Image generation is commented out as it requires additional external dependencies (Playwright) to be installed and configured.

from mermaid import Mermaid
import os

# Define a Mermaid diagram using its text syntax
graph_definition = """
graph TD;
    A[Start]-->B{Process Data};
    B-->C[End];
"""

# Create a Mermaid object
diagram = Mermaid(graph_definition)

# Get the HTML representation to embed or display
html_output = diagram.to_html()
print(f"HTML output starts with: {html_output[:50]}...")

# Or get the JSON representation
json_output = diagram.to_json()
print(f"JSON output (first 50 chars): {json_output[:50]}...")

# To generate an image, you would typically need Playwright installed and configured.
# Example (requires 'pip install playwright' and 'playwright install chromium'):
# if os.environ.get('MERMAID_ENABLE_IMAGE_TEST', 'false').lower() == 'true':
#     try:
#         image_data = diagram.to_image()
#         print(f"Generated image data (bytes length): {len(image_data)}")
#     except Exception as e:
#         print(f"Could not generate image (requires Playwright setup): {e}")

view raw JSON →