|
1
|
|
|
import threading |
|
2
|
|
|
import webbrowser |
|
3
|
|
|
from tempfile import TemporaryDirectory |
|
4
|
|
|
|
|
5
|
|
|
import networkx as nx |
|
6
|
|
|
|
|
7
|
|
|
from networkx_mermaid import DiagramOrientation, DiagramNodeShape |
|
8
|
|
|
from networkx_mermaid.builders import DiagramBuilder |
|
9
|
|
|
from networkx_mermaid.formatters import html, markdown |
|
10
|
|
|
from networkx_mermaid.typing import MermaidDiagram |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
# An example of a graph with multiple components |
|
14
|
|
|
def create_graph(): |
|
15
|
|
|
pastel_colors = ["#FFCCCC", "#CCFFCC", "#CCCCFF", "#FFFFCC", "#CCFFFF", "#FFCCFF"] |
|
16
|
|
|
graphs: list[nx.Graph] = [nx.tetrahedral_graph(), nx.dodecahedral_graph()] |
|
17
|
|
|
|
|
18
|
|
|
for i, g in enumerate(graphs): |
|
19
|
|
|
nx.set_node_attributes(g, {n: {"color": pastel_colors[i]} for n in g.nodes}) |
|
20
|
|
|
|
|
21
|
|
|
graph: nx.Graph = nx.disjoint_union_all(graphs) |
|
22
|
|
|
|
|
23
|
|
|
graph.name = " + ".join(g.name for g in graphs) |
|
24
|
|
|
|
|
25
|
|
|
return graph |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def create_builder(): |
|
29
|
|
|
# Create a Mermaid Diagram Builder with custom settings |
|
30
|
|
|
|
|
31
|
|
|
builder = DiagramBuilder( |
|
32
|
|
|
orientation=DiagramOrientation.LEFT_RIGHT, |
|
33
|
|
|
node_shape=DiagramNodeShape.ROUND_RECTANGLE, |
|
34
|
|
|
) |
|
35
|
|
|
return builder |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def create_server(port: int, root_directory: str, open_browser: bool = True) -> threading.Thread: |
|
39
|
|
|
import http.server |
|
40
|
|
|
import socketserver |
|
41
|
|
|
|
|
42
|
|
|
url = f"http://localhost:{port}" |
|
43
|
|
|
|
|
44
|
|
|
class Handler(http.server.SimpleHTTPRequestHandler): |
|
45
|
|
|
def __init__(self, *args, **kwargs): |
|
46
|
|
|
super().__init__(*args, directory=root_directory, **kwargs) |
|
47
|
|
|
|
|
48
|
|
|
def serve(): |
|
49
|
|
|
with socketserver.TCPServer(('', port), Handler) as httpd: |
|
50
|
|
|
print("Serving at:", url) |
|
51
|
|
|
httpd.serve_forever() |
|
52
|
|
|
|
|
53
|
|
|
server_thread = threading.Thread(target=serve) |
|
54
|
|
|
server_thread.daemon = True |
|
55
|
|
|
server_thread.start() |
|
56
|
|
|
|
|
57
|
|
|
if open_browser: |
|
58
|
|
|
webbrowser.open(url) |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
def main(): |
|
62
|
|
|
graph = create_graph() |
|
63
|
|
|
builder = create_builder() |
|
64
|
|
|
|
|
65
|
|
|
# Build the Mermaid Diagram |
|
66
|
|
|
mermaid_diagram: MermaidDiagram = builder.build(graph) |
|
67
|
|
|
|
|
68
|
|
|
# Format the Mermaid Diagram for Markdown embedding |
|
69
|
|
|
markdown_diagram: str = markdown(mermaid_diagram) |
|
70
|
|
|
|
|
71
|
|
|
# or as single page HTML |
|
72
|
|
|
html_diagram: str = html(mermaid_diagram, title=graph.name) |
|
73
|
|
|
|
|
74
|
|
|
print('Mermaid Diagram:') |
|
75
|
|
|
print(mermaid_diagram) |
|
76
|
|
|
print(markdown_diagram) |
|
77
|
|
|
print(html_diagram) |
|
78
|
|
|
|
|
79
|
|
|
## Save the HTML diagram to a file and serve it |
|
80
|
|
|
with TemporaryDirectory() as temp_dir: |
|
81
|
|
|
with open(f"{temp_dir}/index.html", 'w') as f: |
|
82
|
|
|
f.write(html_diagram) |
|
83
|
|
|
|
|
84
|
|
|
# Serve the HTML diagram |
|
85
|
|
|
create_server(port=8073, root_directory=temp_dir, open_browser=True) |
|
86
|
|
|
|
|
87
|
|
|
# Keep the main thread alive to allow the server to run |
|
88
|
|
|
try: |
|
89
|
|
|
while True: |
|
90
|
|
|
pass |
|
91
|
|
|
except KeyboardInterrupt: |
|
92
|
|
|
print("Server stopped") |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
if __name__ == "__main__": |
|
96
|
|
|
main() |