Skip to main content

Parse Mermaid into manipulable Python objects

Project description

mermaid-py-parser

Parse a mermaid text into editable objects.

Repository link: 20001LastOrder/mermaid-parser-py

Motivation

I love Mermaid syntax for it's simplicity and expressiveness in creating diagrams. And I use them in a couple of my projects. However, I found that the lack of a robust Python library for parsing and manipulating Mermaid diagrams was a significant limitation.

This library packages the original JS Mermaid parser and ported it with PythonMonkey so that the parser can be used in Python. As a result, it can parse almost any mermaid code into structured JSON object. I also plan to continue add converting into mermaid-py objects and NertworkX for eaiser editing and analysis.

Installation

Since this project relies on the original MermaidJS package, you need to have Node.js installed in your system.

Then, You can install the library using pip:

pip install mermaid-parser-py

Or install from source:

poetry install

If you want to rebuild the MermaidJS package, you can run the following:

cd mermaid_parser/js
npm install
npx rollup -c

Basic Usage

Converting Any Mermaid Code Into JSON

from mermaid_parser import MermaidParser

parser = MermaidParser()
json_output = parser.parse("graph TD; A-->B; A-->C; B-->D; C-->D;")
print(json_output)

Converting FlowChart to Mermaid-Py

from mermaid_parser import FlowChartConverter

converter = FlowChartConverter()
flowchart = converter.convert("flowchart TD\nA[Start] --> |Process| B[End]")
print(flowchart)

Known Limitation

To get the internal representation of Mermaid Diagrams, it uses the deprecated mermaidAPI object, which may not be available in future versions of Mermaid.

The MermaidJS diagram does not directly work with serverside Node applications. To make it work, I had to hot-patch the MermaidJS pakcage during packaging time to modify the parts using browser features. See mermaid_parser/js/rollup.config.mjs for more details.

Contribution

Any suggestions, issues, or pull requests are more than welcome 🤗!

Run Tests

You can run the tests using pytest:

pytest tests

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

mermaid_parser_py-0.0.4.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

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

mermaid_parser_py-0.0.4-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

Details for the file mermaid_parser_py-0.0.4.tar.gz.

File metadata

  • Download URL: mermaid_parser_py-0.0.4.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Darwin/25.1.0

File hashes

Hashes for mermaid_parser_py-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1354a6fe182e963d0e8f6e79e58265aaadfdd5ec1594f090edeba81466053212
MD5 88c054d78b4fe03a5871f19fa3585e89
BLAKE2b-256 52562ae3925bc67385daa466cfc20d48b9ba624e3b2f139c34a13d0646f5e1c4

See more details on using hashes here.

File details

Details for the file mermaid_parser_py-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: mermaid_parser_py-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Darwin/25.1.0

File hashes

Hashes for mermaid_parser_py-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 163f5b748f1d5dde389b4d637535dea87fa15ea6506a830b149382bde380457e
MD5 26fa27456249baf015cc7bb8414f794c
BLAKE2b-256 7aa0ab7d6050384d75faf781c28a21aeae0635377d60b070b17a4de710465c17

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