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, and style assignments
  • Export diagrams to Mermaid .mmd or .mermaid files
  • Optional integration with networkx for graph conversion

What's New in

v0.3.0

  • Style class that can be used to add style ... lines to flowcharts
  • Fixes to style-related outputs for nodes

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 }
    B@{ shape: circle, label: End }
    A-->|to end|B
    style A fill:#fff, stroke:#000, stroke-width:2px, color:#000
    style B fill:#fff, stroke:#000, stroke-width:2px, color:#000

That looks like:

---
 title: Example Flowchart
---
flowchart TB
    A@{ shape: rect, label: Start }
    B@{ shape: circle, label: End }
    A-->|to end|B
    style A fill:#fff, stroke:#000, stroke-width:2px, color:#000
    style B fill:#fff, stroke:#000, stroke-width:2px, color:#000

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
    B@{ shape: rect, label: B }
    A@{ shape: rect, label: A }
    A-->|to B|B
    B-->|to A|A
    style A fill:#fff, stroke:#000, stroke-width:2px, color:#000
    style B fill:#fff, stroke:#000, stroke-width:2px, color:#000
---
 title: From NetworkX
---
flowchart LR
    B@{ shape: rect, label: B }
    A@{ shape: rect, label: A }
    A-->|to B|B
    B-->|to A|A
    style A fill:#fff, stroke:#000, stroke-width:2px, color:#000
    style B fill:#fff, stroke:#000, stroke-width:2px, color:#000

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.3.0.tar.gz (7.3 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.3.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mergram-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7594fb3d4f98e3c52ab4fe34f87f3d0c724bb574d3d5cbc4702a91d6cf81c6a4
MD5 b06805e31ef5cf4d680be42ecc19040e
BLAKE2b-256 1581606d6d8de19d01adf2a6bd4cd3d9055014e12a7c4dc5be38ea2dee835b5d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mergram-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4193f57ccc67c921b40162a029f0e8c39e7c9b801b6be0f20381900d446e925c
MD5 66fcfda26d75bc69fa13c6ff5ef701b6
BLAKE2b-256 1985035b26d33bd15e5647867b0d9ae2187aa6b05d7b87e2314ac565cbd0266b

See more details on using hashes here.

Provenance

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