LightRAG/examples/graph_visual_with_html.py

35 lines
819 B
Python
Raw Normal View History

2025-01-27 09:33:35 +01:00
import pipmaster as pm
2025-01-27 23:21:34 +08:00
2025-01-27 09:33:35 +01:00
if not pm.is_installed("pyvis"):
pm.install("pyvis")
2025-02-19 13:47:07 +01:00
if not pm.is_installed("networkx"):
pm.install("networkx")
2025-01-27 09:33:35 +01:00
2025-02-19 13:47:07 +01:00
import networkx as nx
2024-10-20 09:55:52 +08:00
from pyvis.network import Network
import random
2024-10-20 09:55:52 +08:00
# Load the GraphML file
2024-10-25 13:32:25 +05:30
G = nx.read_graphml("./dickens/graph_chunk_entity_relation.graphml")
2024-10-20 09:55:52 +08:00
# Create a Pyvis network
2024-10-26 14:04:11 +08:00
net = Network(height="100vh", notebook=True)
2024-10-20 09:55:52 +08:00
# Convert NetworkX graph to Pyvis network
net.from_nx(G)
# Add colors and title to nodes
for node in net.nodes:
2024-10-25 13:32:25 +05:30
node["color"] = "#{:06x}".format(random.randint(0, 0xFFFFFF))
if "description" in node:
node["title"] = node["description"]
# Add title to edges
for edge in net.edges:
if "description" in edge:
edge["title"] = edge["description"]
2024-10-20 09:55:52 +08:00
# Save and display the network
2024-10-25 13:32:25 +05:30
net.show("knowledge_graph.html")