Visualize a subgraph in Python

I have discovered neo4jupyter package to visualize a graph from neo4j in Jupyter Notebook.

An example:

neo4jupyter.draw(data, {"Drink": "name", "Manufacturer": "name", "Person": "name"})

I also use py2neo for query.

An example:

query = """
MATCH (person:Person)-[:LIKES]->(drink:Drink)
RETURN person.name AS name, drink.name AS drink
"""

data = graph.run(query)
data

however, when I try to visualize this subgraph

neo4jupyter.draw(data, {"Drink": "name", "Manufacturer": "name", "Person": "name"})

It returns Error


AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7728/3103582988.py in
----> 1 neo4jupyter.draw(data, {"Drink": "name", "Manufacturer": "name", "Person": "name"})

D:\Anaconda\lib\site-packages\neo4jupyter\neo4jupyter.py in draw(graph, options, physics, limit)
81 """
82
---> 83 data = graph.run(query, limit=limit)
84
85 nodes =

AttributeError: 'Cursor' object has no attribute 'run'

So how should I visualize/draw the query results? Thanks!

I haven't used neo4jupyter before, neither am I an expert when it comes to py2neo, but it looks like neo4jupyter.draw expects a py2neo Graph object as first parameter, not a Cursor (i.e., query result) object.

So try something along those line

from py2neo import Graph
import neo4jupyter

neo4jupyter.init_notebook_mode()

graph = Graph("bolt://...", auth=("neo4j", "password"))
neo4jupyter.draw(graph, options)

From the "docs":

options should be a dictionary of node labels and property keys; it determines which property is displayed for the node label.
For example, in the movie graph, options = {"Movie": "title", "Person": "name"}. Omitting a node label from the options dict will leave the node unlabeled in the visualization.
Setting physics = True makes the nodes bounce around when you touch them!

1 Like