Skip to main content

Programmatically construct Mermaid diagrams and charts in Python

Project description

MerGram

Programmatically generate Mermaid diagrams and charts using Python.

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Python version badge license release Ruff

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 .mmd or .mermaid files
  • Optional integration with networkx for graph conversion

What's New in

v0.2.0

  • Flowchart.to_markdown function to get a string representation of the flowchart embedded in a Markdown Mermaid code block.
  • Flowchart.to_html function 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


Download files

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

Source Distribution

mergram-0.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

mergram-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

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

Hashes for mergram-0.2.0.tar.gz
Algorithm Hash digest
SHA256 84091a359d99d6bebb242d6e25d35f820f446360aaa40b9549804dc85b45abb3
MD5 358cde2794eb670354cf65d6268667af
BLAKE2b-256 2e9ba5e3c6f9ef3b396767f6282a4263d021c3d575ddad1d5bb7c04ae1786968

See more details on using hashes here.

Provenance

The following attestation bundles were made for mergram-0.2.0.tar.gz:

Publisher: python-publish-pypi.yml on blakeaw/mergram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for mergram-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0abd02c4c2a2f650d2eae6b75ce7651ece3117ce5c50682cceef9c39ccbf97e
MD5 236714926ed6108ce28811bf52857c1b
BLAKE2b-256 72ca77dc4c688c03b74e7cf7748d4ab4b8b3540fce6e1f210eb15fce4a029e74

See more details on using hashes here.

Provenance

The following attestation bundles were made for mergram-0.2.0-py3-none-any.whl:

Publisher: python-publish-pypi.yml on blakeaw/mergram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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