Skip to main content

Create a Mermaid graph from a NetworkX graph

Project description

networkx-mermaid

Create a Mermaid graph from a NetworkX graph

Example

from tempfile import TemporaryDirectory

import networkx as nx

from networkx_mermaid.builders import DiagramBuilder
from networkx_mermaid import DiagramOrientation, DiagramNodeShape
from networkx_mermaid.formatters import html, markdown
from networkx_mermaid.typing import MermaidDiagram


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():
    builder = DiagramBuilder(
        orientation=DiagramOrientation.LEFT_RIGHT,
        node_shape=DiagramNodeShape.ROUND_RECTANGLE,
    )
    return builder


def main():
    graph = create_graph()
    builder = create_builder()

    mermaid_diagram: MermaidDiagram = builder.build(graph)

    markdown_diagram: str = markdown(mermaid_diagram)
    html_diagram: str = html(mermaid_diagram, title=graph.name)

    print('Mermaid Diagram:')
    print(mermaid_diagram)
    print(markdown_diagram)
    print(html_diagram)

    with TemporaryDirectory() as temp_dir:
        with open(f"{temp_dir}/index.html", 'w') as f:
            f.write(html_diagram)

        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()


if __name__ == "__main__":
    main()

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.1.tar.gz (9.4 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.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: networkx_mermaid-0.1.1.tar.gz
  • Upload date:
  • Size: 9.4 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.1.tar.gz
Algorithm Hash digest
SHA256 aeee6f5954d54e6e6995f45eb8fce1af42d4c794d2fc31f7c1f605966ccfa8ed
MD5 99737d03cf3f443b2ea4aad86f6349e5
BLAKE2b-256 e9727152896ce6eeb9eaabc78727d7fcf6b2a9525921710920c17bdb5af90b98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for networkx_mermaid-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39e61ef560d9b04901967d6e8efe023745fa8680f901f94470995cd5bb4cac0e
MD5 8eda90b9959414e102f0e5e6e96c324b
BLAKE2b-256 c68ac4a29cbbe194e88d923c4251f1c1a0e15daadaa9b1fa71fda7fd1b1ad5a7

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