Using Graphify and NetworkX to Map Python Codebase Structure with God Nodes, Communities, and Architecture Visualizations

Using Graphify and NetworkX to Map Python Codebase Structure with God Nodes, Communities, and Architecture Visualizations


plt.figure(figsize=(13, 9))
pos = nx.spring_layout(UG, k=0.7, seed=42)
nx.draw_networkx_edges(UG, pos, alpha=0.25)
nx.draw_networkx_nodes(
   UG, pos,
   node_color=[node_comm.get(n, 0) for n in UG.nodes],
   node_size=[300 + 4000 * deg.get(n, 0) for n in UG.nodes],
   cmap=plt.cm.tab20, alpha=0.9,
)
top = {n for n, _ in sorted(deg.items(), key=lambda x: -x[1])[:14]}
nx.draw_networkx_labels(UG, pos, {n: label(n) for n in top}, font_size=8)
plt.title("Graphify knowledge graph — size=centrality, color=community")
plt.axis("off"); plt.tight_layout()
plt.savefig("graph_static.png", dpi=130); plt.show()
try:
   from pyvis.network import Network
   net = Network(height="650px", width="100%", bgcolor="#111", font_color="white",
                 notebook=True, cdn_resources="in_line", directed=G.is_directed())
   palette = ["#e6194B","#3cb44b","#4363d8","#f58231","#911eb4",
              "#42d4f4","#f032e6","#bfef45","#fabed4","#469990"]
   for n, d in G.nodes(data=True):
       c = node_comm.get(n, 0)
       net.add_node(n, label=label(n), title=f"{d.get('file_type','?')} · {d.get('source_file','')}",
                    color=palette[c % len(palette)], size=12 + 60 * deg.get(n, 0))
   for s, t, d in G.edges(data=True):
       net.add_edge(s, t, title=d.get("relation", ""))
   net.save_graph("graph_interactive.html")
   print("\nSaved interactive graph -> graph_interactive.html")
   from IPython.display import HTML, display
   display(HTML(open("graph_interactive.html").read()))
except Exception as e:
   print("Interactive viz skipped:", e)
for cmd in (
   ["query", "what connects auth to the database?", "--graph", GRAPH_JSON],
   ["path",  "AuthService", "DatabasePool", "--graph", GRAPH_JSON],
   ["explain", "RateLimiter", "--graph", GRAPH_JSON],
):
   print("\n$ graphify " + " ".join(cmd))
   r = subprocess.run([sys.executable, "-m", "graphify", *cmd],
                      capture_output=True, text=True)
   print((r.stdout or r.stderr)[:1200])
print("\nDone. Artifacts: graph_static.png, graph_interactive.html,",
     "and graphify-out/ (graph.json, GRAPH_REPORT.md).")



Source link