How to visualize a Neo4J Graph in python

Viewed 571

I started to learn Neo4J some days ago for a project. I first launched some Cypher query in my imported Graph (~ 6 million nodes) using Neo4J Browser (I'm working locally with bolt) to test my skills, and the software gives me back the nodes and edges graphically.

Now, I need to launch Cypher queries from Python. I have never used python before so I'm using the official documentation to do this job. I'm using neo4j module (from Documentation) to test it and I ran some basic queries successfully. The problem is: I need to "print" the graph obtained from my queries as Neo4J Browser does, but using Python. I searched on google and I have found different results, sometimes they talk about using JS to draw the graph. As said, I'm using a single Python file to work and I'm very unexperienced with this language. Could you suggest me a good library to draw nodes and edges resulted from my query?

Right now this is my code:

def test(self):
     with self.driver.session(database=self.database) as session:
     session.read_transaction(self._print_nodes)

@staticmethod
def _print_nodes(tx):
     result=tx.run("MATCH (n:cell) RETURN n.name, n.id LIMIT 10")
     for record in result:
         print("ID: ",record["n.id"],"| Name: ",record["n.name"])

And this is the result in my console as expected:

enter image description here

But I want something like this:

enter image description here

In this case, I changed the query just to print some nodes, but I hope you got what I mean. I'm going to run some queries like N-[E]->M and I need to see the Node N connected through the edge E to the node M in my GUI. If, between the nodes, more nodes are present, I need to print the whole subgraph which connects N to M, with every Node found in the middle.

2 Answers

I found this on the web and it is also supporting neo4j ver 4>=0 so it is very useful.

https://github.com/merqurio/neo4jupyter

Try installing it first and give it a try. Goodluck.

pip install neo4jupyter

What’s your GUI? If you are using Jupyter or TkinterI’d suggest using the Networkx library to import your graph from Neo4j and output it using Matplotlib. But if you want interaction with the graph, you will have to go with a JS library like Vis. In fact, I just checked out the other answer and neo4jupyter uses Vis under the hood. So that would take care of the interaction requirements.

Related