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

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: 2px }
    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.1.0.tar.gz (6.1 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.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file mergram-0.1.0.tar.gz.

File metadata

  • Download URL: mergram-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mergram-0.1.0.tar.gz
Algorithm Hash digest
SHA256 31fa4987891df2c2b67e6dadeda6b8a7e523baba3e4675949ace0557e8f15e54
MD5 4a74608d75b4039db4c6d65c0caec825
BLAKE2b-256 6e1cb4eb418f242c6a027d055426e73570f525aefce62366514c4b7448ef0db6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mergram-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: mergram-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b81058e0022f134857b053d8f31d7aacdfb1de27574756bc8d9610883da2ae0c
MD5 5f42fdd94df62aa1eb130bdd904472ab
BLAKE2b-256 fc1151fe1c576104cc3396ca5939b0098e628d026be72ca190eb5a26daf6d782

See more details on using hashes here.

Provenance

The following attestation bundles were made for mergram-0.1.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