Skip to main content

Create a Mermaid graph from a NetworkX graph

Project description

networkx-mermaid

Create a Mermaid graph from a NetworkX graph

Quick Start

from tempfile import TemporaryDirectory

import networkx as nx

from src import networkx_mermaid as nxm

# colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF']
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)

mermaid_diagram = nxm.mermaid(graph,
                              orientation=nxm.Orientation.LEFT_RIGHT,
                              node_shape=nxm.NodeShape.ROUND_RECTANGLE)

with TemporaryDirectory() as temp_dir:
    with open(f"{temp_dir}/index.html", 'w') as f:
        rendered = nxm.html(mermaid_diagram, title=graph.name)
        f.write(rendered)

    import http.server
    import socketserver

    PORT = 8073


    class Handler(http.server.SimpleHTTPRequestHandler):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, directory=temp_dir, **kwargs)


    with socketserver.TCPServer(("", PORT), Handler) as httpd:
        print("serving at port", PORT)
        httpd.serve_forever()

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

networkx_mermaid-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

networkx_mermaid-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for networkx_mermaid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8016c904922057406151e40c0fb313c2b47ab47205d38786909efbe2939125cc
MD5 9877c3a8cc7971ccfed34fe43d69f92e
BLAKE2b-256 892462d7388997c3756be9642676968805e5c20dc856979be811c2657c3ebfbc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for networkx_mermaid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c62b351fc60867eb1165f827cd00830554f74e70f17b296ad887171c4e553ae4
MD5 30c98b1d64b12ea426236607dac0244a
BLAKE2b-256 8e11c146cb79da2b6f5772b9368fd6496c5cbda97e7ac52d76318b4f42b9203f

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