Programmatically construct Mermaid diagrams and charts in Python
Project description
MerGram
Programmatically generate Mermaid diagrams and charts using Python.
MerGram currently supports constructing Mermaid flowcharts, with plans to expand support for additional diagram and chart types in future releases.
Features
- Build Mermaid flowcharts using Python classes
- Support for nodes, links, and subgraphs
- Export diagrams to Mermaid
.mmdor.mermaidfiles - Optional integration with networkx for graph conversion
What's New in
v0.2.0
Flowchart.to_markdownfunction to get a string representation of the flowchart embedded in a Markdown Mermaid code block.Flowchart.to_htmlfunction to get a string representation of the flowchart embedded in HTML.
Installation
pip install mergram
To enable networkx integration:
pip install mergram[networkx]
Usage
Basic Flowchart
from mergram.flowchart import Flowchart, Node, Link
fc = Flowchart("Example Flowchart")
a = Node("A", label="Start")
b = Node("B", label="End", shape='circle')
fc += a
fc += b
fc += Link(a, b, text="to end")
print(fc)
Generates the following mermaid diagram code:
---
title: Example Flowchart
---
flowchart TB
A@{ shape: rect, label: Start, fill: #fff, stroke: #000, stroke-width: 2px }
B@{ shape: circle, label: End, fill: #fff, stroke: #000, stroke-width: 2px }
A-->|to end|B
That looks like:
---
title: Example Flowchart
---
flowchart TB
A@{ shape: rect, label: Start, fill: #fff, stroke: #000, stroke-width: 4px }
B@{ shape: circle, label: End, fill: #fff, stroke: #000, stroke-width: 2px }
A-->|to end|B
From NetworkX
import networkx as nx
from mergram.flowchart import Flowchart
G = nx.DiGraph()
G.add_edge("A", "B", label="to B")
G.add_edge("B", "A", label="to A")
fc = Flowchart("From NetworkX", orientation='LR')
fc.from_networkx(G)
print(fc)
---
title: From NetworkX
---
flowchart LR
A@{ shape: rect, label: A, fill: #fff, stroke: #000, stroke-width: 2px }
B@{ shape: rect, label: B, fill: #fff, stroke: #000, stroke-width: 2px }
A-->|to B|B
B-->|to A|A
---
title: From NetworkX
---
flowchart LR
A@{ shape: rect, label: A, fill: #fff, stroke: #000, stroke-width: 2px }
B@{ shape: rect, label: B, fill: #fff, stroke: #000, stroke-width: 2px }
A-->|to B|B
B-->|to A|A
Export to File
fc.write("diagram.mmd")
Optional Dependencies
networkx: For converting existing graphs to Mermaid flowcharts.
License
MIT License
Contributing
Contributions are welcome! Please open issues or pull requests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mergram-0.2.0.tar.gz.
File metadata
- Download URL: mergram-0.2.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84091a359d99d6bebb242d6e25d35f820f446360aaa40b9549804dc85b45abb3
|
|
| MD5 |
358cde2794eb670354cf65d6268667af
|
|
| BLAKE2b-256 |
2e9ba5e3c6f9ef3b396767f6282a4263d021c3d575ddad1d5bb7c04ae1786968
|
Provenance
The following attestation bundles were made for mergram-0.2.0.tar.gz:
Publisher:
python-publish-pypi.yml on blakeaw/mergram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mergram-0.2.0.tar.gz -
Subject digest:
84091a359d99d6bebb242d6e25d35f820f446360aaa40b9549804dc85b45abb3 - Sigstore transparency entry: 268392016
- Sigstore integration time:
-
Permalink:
blakeaw/mergram@5ff0896fc2d55deda86f8287decfcde38a8ef1eb -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/blakeaw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-pypi.yml@5ff0896fc2d55deda86f8287decfcde38a8ef1eb -
Trigger Event:
release
-
Statement type:
File details
Details for the file mergram-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mergram-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0abd02c4c2a2f650d2eae6b75ce7651ece3117ce5c50682cceef9c39ccbf97e
|
|
| MD5 |
236714926ed6108ce28811bf52857c1b
|
|
| BLAKE2b-256 |
72ca77dc4c688c03b74e7cf7748d4ab4b8b3540fce6e1f210eb15fce4a029e74
|
Provenance
The following attestation bundles were made for mergram-0.2.0-py3-none-any.whl:
Publisher:
python-publish-pypi.yml on blakeaw/mergram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mergram-0.2.0-py3-none-any.whl -
Subject digest:
c0abd02c4c2a2f650d2eae6b75ce7651ece3117ce5c50682cceef9c39ccbf97e - Sigstore transparency entry: 268392018
- Sigstore integration time:
-
Permalink:
blakeaw/mergram@5ff0896fc2d55deda86f8287decfcde38a8ef1eb -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/blakeaw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-pypi.yml@5ff0896fc2d55deda86f8287decfcde38a8ef1eb -
Trigger Event:
release
-
Statement type: