Skip to main content

A Python helper library to create Mermaid.js diagrams

Project description

Alright — I’ll update your README.md so it:

  • Explains .mmd files
  • Includes the utils.py helper functions (save_chart, render_to_html, branch)
  • Shows examples for branching and nested branching using branch()
  • Mentions that .mmd files can be previewed in editors and docs tools

Updated README.md

# py2mermaid

py2mermaid is a Python library that makes it easy to generate [Mermaid.js](https://mermaid.js.org/) diagrams directly from Python.

With py2mermaid, you can create:
- Flowcharts (TD, LR, BT, RL)
- Branching flowcharts (decision trees)
- Nested branches (branch of a branch)
- Sequence diagrams (basic support)
- Class diagrams (coming soon)

Mermaid.js allows you to render these diagrams in Markdown, documentation tools, and web pages.

---

## 📦 Installation

### From PyPI
```bash
pip install py2mermaid

From GitHub

pip install git+https://github.com/yourusername/py2mermaid.git

📂 What is .mmd?

A .mmd file is a plain text file containing Mermaid diagram syntax. Example: flowchart TD followed by your nodes and edges.

Example example_chart.mmd:

flowchart TD
A[Start]
B[Process]
C[End]
A --> B
B --> C

You can:

  • Open it in Mermaid Live Editor
  • Use it in GitHub Markdown
  • Embed it in static site generators (MkDocs, Docusaurus, etc.)
  • Preview it in editors like VS Code (with Mermaid plugin)

🚀 Quick Start

from py2mermaid.chart import MermaidChart

chart = MermaidChart(chart_type="flowchart", direction="TD")
chart.add_node("A", "Start")
chart.add_node("B", "Process")
chart.add_node("C", "End")
chart.add_edge("A", "B", "step 1")
chart.add_edge("B", "C", "step 2")

print(chart.render())

Output Mermaid code:

flowchart TD
A[Start]
B[Process]
C[End]
A -->|step 1| B
B -->|step 2| C

🛠 Using Utility Functions

The utils.py file includes:

  • save_chart(chart, filename) → Saves Mermaid code to .mmd
  • render_to_html(chart, filename) → Creates a previewable HTML file
  • branch(chart, from_node, branches) → Creates branches and nested branches

1️⃣ Save a Chart as .mmd

from py2mermaid.chart import MermaidChart
from py2mermaid.utils import save_chart

chart = MermaidChart("flowchart", "LR")
chart.add_node("A", "Start")
chart.add_node("B", "Do Work")
chart.add_edge("A", "B")

save_chart(chart, "my_chart")  # Saves as my_chart.mmd

2️⃣ Render to HTML for Preview

from py2mermaid.utils import render_to_html

render_to_html(chart, "my_chart")  # Saves as my_chart.html
# Open in browser to view

3️⃣ Branching Flowchart (Decision Tree)

from py2mermaid.chart import MermaidChart
from py2mermaid.utils import branch

chart = MermaidChart("flowchart", "TD")
chart.add_node("A", "Start")

branch(chart, "A", [
    ("B", "Option 1"),
    ("C", "Option 2")
])

print(chart.render())

Output:

flowchart TD
A[Start]
B[Option 1]
C[Option 2]
A --> B
A --> C

4️⃣ Nested Branches (Branch of a Branch)

chart = MermaidChart("flowchart", "TD")
chart.add_node("A", "Start")

branch(chart, "A", [
    ("B", "Path 1", [
        ("B1", "Path 1A"),
        ("B2", "Path 1B")
    ]),
    ("C", "Path 2")
])

print(chart.render())

Output:

flowchart TD
A[Start]
B[Path 1]
B1[Path 1A]
B2[Path 1B]
C[Path 2]
A --> B
B --> B1
B --> B2
A --> C

💡 Tips

  • py2mermaid generates code — rendering is done by Mermaid.js in browsers or Markdown processors.
  • Save .mmd files to reuse diagrams in docs.
  • Use render_to_html() for instant browser preview.

📜 License

This project is licensed under the MIT License.


🤝 Contributing

  1. Fork the repo
  2. Create a new branch: git checkout -b feature-name
  3. Make changes and commit: git commit -m 'Added new feature'
  4. Push to branch: git push origin feature-name
  5. Open a Pull Request

---

I’ve now included:
- `.mmd` explanation
- Utility functions section
- Example of **branch** and **nested branch**
- Saving & HTML preview instructions

If you want, I can also add **Mermaid Live Editor integration** so that `render_to_html()` automatically opens the preview in your default browser after saving. That would make it even smoother for testing.  

Do you want me to add that?

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

py2mermaid-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

py2mermaid-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py2mermaid-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for py2mermaid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d949f1876533149c510a5c7c31e66f2cb5a1869c4cabdbbc9c2abdb7fdcbc90d
MD5 8ea9315690aa95760d87aef47de5017e
BLAKE2b-256 ba6e4b227a09c84abee4ec5d7330047385448aabc60b3ec7f901c44affb05a71

See more details on using hashes here.

File details

Details for the file py2mermaid-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py2mermaid-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for py2mermaid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2297d1c8ecd45672d68f292932c37ff927033a2964028799302fe78b40ce8c63
MD5 2e98ab7539ca97c145e271fc66c64332
BLAKE2b-256 b0b174213bfae8f43d2eccf1b1217d547882ffb36e2456c5a202c7a78efa7b93

See more details on using hashes here.

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