graph_visual_with_html.py 819 B

12345678910111213141516171819202122232425262728293031323334
  1. import pipmaster as pm
  2. if not pm.is_installed("pyvis"):
  3. pm.install("pyvis")
  4. if not pm.is_installed("networkx"):
  5. pm.install("networkx")
  6. import networkx as nx
  7. from pyvis.network import Network
  8. import random
  9. # Load the GraphML file
  10. G = nx.read_graphml("./dickens/graph_chunk_entity_relation.graphml")
  11. # Create a Pyvis network
  12. net = Network(height="100vh", notebook=True)
  13. # Convert NetworkX graph to Pyvis network
  14. net.from_nx(G)
  15. # Add colors and title to nodes
  16. for node in net.nodes:
  17. node["color"] = "#{:06x}".format(random.randint(0, 0xFFFFFF))
  18. if "description" in node:
  19. node["title"] = node["description"]
  20. # Add title to edges
  21. for edge in net.edges:
  22. if "description" in edge:
  23. edge["title"] = edge["description"]
  24. # Save and display the network
  25. net.show("knowledge_graph.html")