You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
43 lines
1.1 KiB
import pipmaster as pm
|
|
|
|
|
|
if not pm.is_installed("pyvis"):
|
|
pm.install("pyvis>=0.3.1")
|
|
if not pm.is_installed("networkx"):
|
|
pm.install("networkx")
|
|
|
|
import networkx as nx
|
|
from pyvis.network import Network
|
|
import random
|
|
|
|
# Load the GraphML file
|
|
G = nx.read_graphml("../Topic/ChuZhongShuXue/graph_chunk_entity_relation.graphml")
|
|
|
|
# Create a Pyvis network
|
|
net = Network(height="100vh", notebook=True)
|
|
# Convert NetworkX graph to Pyvis network
|
|
net.from_nx(G)
|
|
|
|
|
|
# Add colors and title to nodes
|
|
for node in net.nodes:
|
|
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"]
|
|
|
|
|
|
html_path = "knowledge_graph.html"
|
|
|
|
# Replace CDN URL from cdn.jsdelivr.net to fastly.jsdelivr.net
|
|
with open(html_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
content = content.replace("cdn.jsdelivr.net", "fastly.jsdelivr.net")
|
|
with open(html_path, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
|
|
net.show(html_path) |