Parse a mermaid text into editable objects.
Repository link: 20001LastOrder/mermaid-parser-py
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.
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
from mermaid_parser import MermaidParser
parser = MermaidParser()
json_output = parser.parse("graph TD; A-->B; A-->C; B-->D; C-->D;")
print(json_output)
from mermaid_parser import FlowChartConverter
converter = FlowChartConverter()
flowchart = converter.convert("flowchart TD\nA[Start] --> |Process| B[End]")
print(flowchart)
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.
Any suggestions, issues, or pull requests are more than welcome 🤗!