Name | networkx-mermaid JSON |
Version |
0.1.3
JSON |
| download |
home_page | None |
Summary | Create a Mermaid graph from a NetworkX graph |
upload_time | 2025-03-11 23:28:56 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.10 |
license | None |
keywords |
graph
mermaid
networkx
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
# networkx-mermaid
Create a Mermaid graph from a NetworkX graph
[](https://codecov.io/gh/erivlis/networkx-mermaid)
[](https://app.codacy.com/gh/erivlis/networkx-mermaid/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
[](https://app.codacy.com/gh/erivlis/networkx-mermaid?utm_source=github.com&utm_medium=referral&utm_content=erivlis/networkx-mermaid&utm_campaign=Badge_Grade)
[](https://www.codefactor.io/repository/github/erivlis/networkx-mermaid)
[](https://scrutinizer-ci.com/g/erivlis/networkx-mermaid/?branch=main)
[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)
[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)
[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)
[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)
[](https://snyk.io/test/github/erivlis/networkx-mermaid)
## Example
```python title="Create a Mermaid Diagram from a NetworkX Graph"
import threading
import webbrowser
from tempfile import TemporaryDirectory
import networkx as nx
from networkx_mermaid import DiagramOrientation, DiagramNodeShape
from networkx_mermaid.builders import DiagramBuilder
from networkx_mermaid.formatters import html, markdown
from networkx_mermaid.typing import MermaidDiagram
# An example of a graph with multiple components
def create_graph():
pastel_colors = ["#FFCCCC", "#CCFFCC", "#CCCCFF", "#FFFFCC", "#CCFFFF", "#FFCCFF"]
graphs: list[nx.Graph] = [nx.tetrahedral_graph(), nx.dodecahedral_graph()]
for i, g in enumerate(graphs):
nx.set_node_attributes(g, {n: {"color": pastel_colors[i]} for n in g.nodes})
graph: nx.Graph = nx.disjoint_union_all(graphs)
graph.name = " + ".join(g.name for g in graphs)
return graph
def create_builder():
# Create a Mermaid Diagram Builder with custom settings
builder = DiagramBuilder(
orientation=DiagramOrientation.LEFT_RIGHT,
node_shape=DiagramNodeShape.ROUND_RECTANGLE,
)
return builder
def create_server(port: int, root_directory: str, open_browser: bool = True) -> threading.Thread:
import http.server
import socketserver
url = f"http://localhost:{port}"
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=root_directory, **kwargs)
def serve():
with socketserver.TCPServer(('', port), Handler) as httpd:
print("Serving at:", url)
httpd.serve_forever()
server_thread = threading.Thread(target=serve)
server_thread.daemon = True
server_thread.start()
if open_browser:
webbrowser.open(url)
def main():
graph = create_graph()
builder = create_builder()
# Build the Mermaid Diagram
mermaid_diagram: MermaidDiagram = builder.build(graph)
# Format the Mermaid Diagram for Markdown embedding
markdown_diagram: str = markdown(mermaid_diagram)
# or as single page HTML
html_diagram: str = html(mermaid_diagram, title=graph.name)
print('Mermaid Diagram:')
print(mermaid_diagram)
print(markdown_diagram)
print(html_diagram)
## Save the HTML diagram to a file and serve it
with TemporaryDirectory() as temp_dir:
with open(f"{temp_dir}/index.html", 'w') as f:
f.write(html_diagram)
# Serve the HTML diagram
create_server(port=8073, root_directory=temp_dir, open_browser=True)
# Keep the main thread alive to allow the server to run
try:
while True:
pass
except KeyboardInterrupt:
print("Server stopped")
if __name__ == "__main__":
main()
```
## Diagram
```mermaid
---
title: Platonic Tetrahedral Graph + Dodecahedral Graph
config:
layout: dagre
look: neo
theme: neutral
---
graph LR
AAA([0])
style AAA fill:#FFCCCC, color:#000000
AAE([1])
style AAE fill:#FFCCCC, color:#000000
AAI([2])
style AAI fill:#FFCCCC, color:#000000
AAM([3])
style AAM fill:#FFCCCC, color:#000000
AAQ([4])
style AAQ fill:#CCFFCC, color:#000000
AAU([5])
style AAU fill:#CCFFCC, color:#000000
AAY([6])
style AAY fill:#CCFFCC, color:#000000
AAc([7])
style AAc fill:#CCFFCC, color:#000000
AAg([8])
style AAg fill:#CCFFCC, color:#000000
AAk([9])
style AAk fill:#CCFFCC, color:#000000
AAo([10])
style AAo fill:#CCFFCC, color:#000000
AAs([11])
style AAs fill:#CCFFCC, color:#000000
AAw([12])
style AAw fill:#CCFFCC, color:#000000
AA0([13])
style AA0 fill:#CCFFCC, color:#000000
AA4([14])
style AA4 fill:#CCFFCC, color:#000000
AA8([15])
style AA8 fill:#CCFFCC, color:#000000
ABA([16])
style ABA fill:#CCFFCC, color:#000000
ABE([17])
style ABE fill:#CCFFCC, color:#000000
ABI([18])
style ABI fill:#CCFFCC, color:#000000
ABM([19])
style ABM fill:#CCFFCC, color:#000000
ABQ([20])
style ABQ fill:#CCFFCC, color:#000000
ABU([21])
style ABU fill:#CCFFCC, color:#000000
ABY([22])
style ABY fill:#CCFFCC, color:#000000
ABc([23])
style ABc fill:#CCFFCC, color:#000000
AAA --> AAE
AAA --> AAI
AAA --> AAM
AAE --> AAI
AAE --> AAM
AAI --> AAM
AAQ --> AAU
AAQ --> ABc
AAQ --> AA4
AAU --> AAY
AAU --> AAw
AAY --> AAc
AAY --> AAo
AAc --> AAg
AAc --> ABc
AAg --> AAk
AAg --> ABU
AAk --> AAo
AAk --> ABM
AAo --> AAs
AAs --> AAw
AAs --> ABI
AAw --> AA0
AA0 --> AA4
AA0 --> ABE
AA4 --> AA8
AA8 --> ABA
AA8 --> ABY
ABA --> ABE
ABA --> ABQ
ABE --> ABI
ABI --> ABM
ABM --> ABQ
ABQ --> ABU
ABU --> ABY
ABY --> ABc
```
Raw data
{
"_id": null,
"home_page": null,
"name": "networkx-mermaid",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "Graph, Mermaid, NetworkX",
"author": null,
"author_email": "Eran Rivlis <eran@rivlis.info>",
"download_url": "https://files.pythonhosted.org/packages/ce/8c/cc84888dda511674b8c02c0ddb3e9e867c881eabc4c5b68c2b5d4e5b980e/networkx_mermaid-0.1.3.tar.gz",
"platform": null,
"description": "# networkx-mermaid\n\nCreate a Mermaid graph from a NetworkX graph\n\n[](https://codecov.io/gh/erivlis/networkx-mermaid)\n[](https://app.codacy.com/gh/erivlis/networkx-mermaid/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)\n[](https://app.codacy.com/gh/erivlis/networkx-mermaid?utm_source=github.com&utm_medium=referral&utm_content=erivlis/networkx-mermaid&utm_campaign=Badge_Grade)\n[](https://www.codefactor.io/repository/github/erivlis/networkx-mermaid)\n[](https://scrutinizer-ci.com/g/erivlis/networkx-mermaid/?branch=main)\n\n[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)\n[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)\n[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)\n[](https://sonarcloud.io/summary/new_code?id=erivlis_networkx-mermaid)\n\n[](https://snyk.io/test/github/erivlis/networkx-mermaid)\n\n## Example\n\n```python title=\"Create a Mermaid Diagram from a NetworkX Graph\"\nimport threading\nimport webbrowser\nfrom tempfile import TemporaryDirectory\n\nimport networkx as nx\n\nfrom networkx_mermaid import DiagramOrientation, DiagramNodeShape\nfrom networkx_mermaid.builders import DiagramBuilder\nfrom networkx_mermaid.formatters import html, markdown\nfrom networkx_mermaid.typing import MermaidDiagram\n\n\n# An example of a graph with multiple components\ndef create_graph():\n pastel_colors = [\"#FFCCCC\", \"#CCFFCC\", \"#CCCCFF\", \"#FFFFCC\", \"#CCFFFF\", \"#FFCCFF\"]\n graphs: list[nx.Graph] = [nx.tetrahedral_graph(), nx.dodecahedral_graph()]\n\n for i, g in enumerate(graphs):\n nx.set_node_attributes(g, {n: {\"color\": pastel_colors[i]} for n in g.nodes})\n\n graph: nx.Graph = nx.disjoint_union_all(graphs)\n\n graph.name = \" + \".join(g.name for g in graphs)\n\n return graph\n\n\ndef create_builder():\n # Create a Mermaid Diagram Builder with custom settings\n\n builder = DiagramBuilder(\n orientation=DiagramOrientation.LEFT_RIGHT,\n node_shape=DiagramNodeShape.ROUND_RECTANGLE,\n )\n return builder\n\n\ndef create_server(port: int, root_directory: str, open_browser: bool = True) -> threading.Thread:\n import http.server\n import socketserver\n\n url = f\"http://localhost:{port}\"\n\n class Handler(http.server.SimpleHTTPRequestHandler):\n def __init__(self, *args, **kwargs):\n super().__init__(*args, directory=root_directory, **kwargs)\n\n def serve():\n with socketserver.TCPServer(('', port), Handler) as httpd:\n print(\"Serving at:\", url)\n httpd.serve_forever()\n\n server_thread = threading.Thread(target=serve)\n server_thread.daemon = True\n server_thread.start()\n\n if open_browser:\n webbrowser.open(url)\n\n\ndef main():\n graph = create_graph()\n builder = create_builder()\n\n # Build the Mermaid Diagram\n mermaid_diagram: MermaidDiagram = builder.build(graph)\n\n # Format the Mermaid Diagram for Markdown embedding\n markdown_diagram: str = markdown(mermaid_diagram)\n\n # or as single page HTML\n html_diagram: str = html(mermaid_diagram, title=graph.name)\n\n print('Mermaid Diagram:')\n print(mermaid_diagram)\n print(markdown_diagram)\n print(html_diagram)\n\n ## Save the HTML diagram to a file and serve it\n with TemporaryDirectory() as temp_dir:\n with open(f\"{temp_dir}/index.html\", 'w') as f:\n f.write(html_diagram)\n\n # Serve the HTML diagram\n create_server(port=8073, root_directory=temp_dir, open_browser=True)\n\n # Keep the main thread alive to allow the server to run\n try:\n while True:\n pass\n except KeyboardInterrupt:\n print(\"Server stopped\")\n\n\nif __name__ == \"__main__\":\n main()\n```\n\n## Diagram\n\n```mermaid\n---\ntitle: Platonic Tetrahedral Graph + Dodecahedral Graph\nconfig:\n layout: dagre\n look: neo\n theme: neutral\n---\ngraph LR\nAAA([0])\nstyle AAA fill:#FFCCCC, color:#000000\nAAE([1])\nstyle AAE fill:#FFCCCC, color:#000000\nAAI([2])\nstyle AAI fill:#FFCCCC, color:#000000\nAAM([3])\nstyle AAM fill:#FFCCCC, color:#000000\nAAQ([4])\nstyle AAQ fill:#CCFFCC, color:#000000\nAAU([5])\nstyle AAU fill:#CCFFCC, color:#000000\nAAY([6])\nstyle AAY fill:#CCFFCC, color:#000000\nAAc([7])\nstyle AAc fill:#CCFFCC, color:#000000\nAAg([8])\nstyle AAg fill:#CCFFCC, color:#000000\nAAk([9])\nstyle AAk fill:#CCFFCC, color:#000000\nAAo([10])\nstyle AAo fill:#CCFFCC, color:#000000\nAAs([11])\nstyle AAs fill:#CCFFCC, color:#000000\nAAw([12])\nstyle AAw fill:#CCFFCC, color:#000000\nAA0([13])\nstyle AA0 fill:#CCFFCC, color:#000000\nAA4([14])\nstyle AA4 fill:#CCFFCC, color:#000000\nAA8([15])\nstyle AA8 fill:#CCFFCC, color:#000000\nABA([16])\nstyle ABA fill:#CCFFCC, color:#000000\nABE([17])\nstyle ABE fill:#CCFFCC, color:#000000\nABI([18])\nstyle ABI fill:#CCFFCC, color:#000000\nABM([19])\nstyle ABM fill:#CCFFCC, color:#000000\nABQ([20])\nstyle ABQ fill:#CCFFCC, color:#000000\nABU([21])\nstyle ABU fill:#CCFFCC, color:#000000\nABY([22])\nstyle ABY fill:#CCFFCC, color:#000000\nABc([23])\nstyle ABc fill:#CCFFCC, color:#000000\nAAA --> AAE\nAAA --> AAI\nAAA --> AAM\nAAE --> AAI\nAAE --> AAM\nAAI --> AAM\nAAQ --> AAU\nAAQ --> ABc\nAAQ --> AA4\nAAU --> AAY\nAAU --> AAw\nAAY --> AAc\nAAY --> AAo\nAAc --> AAg\nAAc --> ABc\nAAg --> AAk\nAAg --> ABU\nAAk --> AAo\nAAk --> ABM\nAAo --> AAs\nAAs --> AAw\nAAs --> ABI\nAAw --> AA0\nAA0 --> AA4\nAA0 --> ABE\nAA4 --> AA8\nAA8 --> ABA\nAA8 --> ABY\nABA --> ABE\nABA --> ABQ\nABE --> ABI\nABI --> ABM\nABM --> ABQ\nABQ --> ABU\nABU --> ABY\nABY --> ABc\n```",
"bugtrack_url": null,
"license": null,
"summary": "Create a Mermaid graph from a NetworkX graph",
"version": "0.1.3",
"project_urls": {
"Bug Tracker": "https://github.com/erivlis/networkx-mermaid/issues",
"Documentation": "https://github.com/erivlis/networkx-mermaid",
"Homepage": "https://github.com/erivlis/networkx-mermaid",
"Source": "https://github.com/erivlis/networkx-mermaid"
},
"split_keywords": [
"graph",
" mermaid",
" networkx"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "5889b0efeb7f96c10524bbf218409d3103ea224d94b8d417a0f5029bf93387fd",
"md5": "bcaac4a3bb668ef1d502716ca9a1a9fc",
"sha256": "b46c830b03ab9f384ec07e0950a393893c870a0bc90f1ddc1327b899ae0c2c99"
},
"downloads": -1,
"filename": "networkx_mermaid-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bcaac4a3bb668ef1d502716ca9a1a9fc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 7556,
"upload_time": "2025-03-11T23:28:55",
"upload_time_iso_8601": "2025-03-11T23:28:55.637875Z",
"url": "https://files.pythonhosted.org/packages/58/89/b0efeb7f96c10524bbf218409d3103ea224d94b8d417a0f5029bf93387fd/networkx_mermaid-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "ce8ccc84888dda511674b8c02c0ddb3e9e867c881eabc4c5b68c2b5d4e5b980e",
"md5": "d3455c8611f529def61d268c2db50749",
"sha256": "3efa59de2268035855e1f3c2ce57768ff531304becea2a504550cf31a0a5df20"
},
"downloads": -1,
"filename": "networkx_mermaid-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "d3455c8611f529def61d268c2db50749",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 10944,
"upload_time": "2025-03-11T23:28:56",
"upload_time_iso_8601": "2025-03-11T23:28:56.461570Z",
"url": "https://files.pythonhosted.org/packages/ce/8c/cc84888dda511674b8c02c0ddb3e9e867c881eabc4c5b68c2b5d4e5b980e/networkx_mermaid-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-03-11 23:28:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "erivlis",
"github_project": "networkx-mermaid",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "networkx-mermaid"
}