Examples

Graph Visualization

Visualize your graph in Jupyter notebooks or as HTML files.

CogDB can render your graph as an interactive visualization.

Creating a Graph

from cog.torque import Graph

g = Graph("people")
g.put("alice", "follows", "bob")
g.put("bob", "follows", "fred")
g.put("bob", "status", "cool_person")
g.put("charlie", "follows", "bob")
g.put("charlie", "follows", "dani")
g.put("dani", "follows", "bob")
g.put("dani", "follows", "greg")
g.put("dani", "status", "cool_person")
g.put("emily", "follows", "fred")
g.put("fred", "follows", "greg")
g.put("greg", "status", "cool_person")

Visualizing in Jupyter

Use view() and render() to visualize relationships:

g.v().tag("from").out("follows").tag("to").view("follows").render()

render() works in Jupyter notebooks. Otherwise use view(..).url to get the HTML file path.

rendered graph

Visualization Steps

  1. tag("from") - Tag the source nodes
  2. .out("follows") - Traverse the relationship
  3. tag("to") - Tag the destination nodes
  4. .view("follows") - Create a view with the edge label
  5. .render() - Render in Jupyter

Required Tags for Rendering

When using .view() to render graph visualizations, you must use specific tag names:

Tag NamePurpose
fromIdentifies the source node of each edge
toIdentifies the target node of each edge

The graph visualization reads the from and to properties from each result to construct nodes and edges.

Important Notes:

  • Tag names are case-sensitive - use lowercase from and to
  • Both tags are required - the view needs both to draw edges between nodes
  • Order matters - from should mark the source vertex and to should mark the destination vertex in your traversal
  • Other tags are ignored - you can add additional tags (e.g., tag("source")), but only from and to are used for rendering

Export to HTML

Outside of Jupyter, get the HTML file path:

viz = g.v().tag("from").out("follows").tag("to").view("follows")
print(viz.url)  # Path to the HTML file

On this page