Mermaid Builder
Mermaid Builder is a Python library designed to programmatically generate MermaidJS markup, facilitating the creation of various diagrams like flowcharts and sequence diagrams from Python code. It allows developers to script the creation of MermaidJS definitions, which can then be rendered in compatible environments such as Markdown files, GitHub, or online Mermaid playgrounds. The library is in early development, with its current version being 0.0.3, indicating rapid iteration and ongoing feature additions.
Warnings
- breaking The library is in early development (version 0.0.3) and explicitly marked as 'Work in progress'. This means its API is subject to frequent changes, and backward compatibility is not guaranteed between minor versions.
- gotcha This library generates MermaidJS *markup*, but does not render the diagrams itself. To visualize the diagrams, you will need a separate Mermaid.js rendering engine (e.g., a web browser with `mermaid.js` loaded, a Markdown viewer like GitHub, or the `mmdc` CLI tool which requires Node.js).
- gotcha The PyPI project description lists several 'Next tasks' such as 'styling for flowchart (adding classes and styles)' and 'class diagrams'. This indicates that certain advanced MermaidJS features might not yet be fully implemented or supported by `mermaid-builder`.
- gotcha When MermaidJS diagrams are rendered in applications with dynamic theming (e.g., dark/light modes in some markdown editors), custom colors defined in the generated markup might be unexpectedly inverted or altered due to CSS filters applied by the rendering environment.
Install
-
pip install mermaid-builder
Imports
- Chart
from mermaid_builder.flowchart import Chart
- Node
from mermaid_builder.flowchart import Node
Quickstart
from mermaid_builder.flowchart import Chart, Node
# Create a new flowchart
chart = Chart("MyCompanyHierarchy", direction="TD") # Top Down direction
# Add nodes
node_a = Node("Company A")
node_b = Node("Company B")
node_c = Node("Company C")
node_d = Node("Company D")
chart.add_node(node_a)
chart.add_node(node_b)
chart.add_node(node_c)
chart.add_node(node_d)
# Define relationships
chart.link(node_a, node_b)
chart.link(node_a, node_c)
chart.link(node_b, node_d)
# Generate the MermaidJS markup
mermaid_markup = chart.render()
print(mermaid_markup)
# To view this diagram, paste the output into a MermaidJS editor (e.g., mermaid.live)
# or use in a Markdown file that supports Mermaid rendering (e.g., GitHub).