Neo4j database
The neo4j-viz library provides a convenience method for importing data from the Neo4j database via the Neo4j Python driver.
It requires and additional dependency to be installed, which you can do by running:
pip install neo4j-viz[neo4j]
Once you have installed the additional dependency, you can use the from_neo4j method
to import query results from Neo4j.
The from_neo4j method takes one mandatory positional parameter: A data argument representing either a query result in the shape of a neo4j.graph.Graph or neo4j.Result, or a neo4j.Driver in which case a simple default query will be executed internally to retrieve the graph data.
The optional max_rows parameter can be used to limit the number of relationships shown in the visualization.
By default, it is set to 10.000, meaning that if the database has more than 10.000 rows, a warning will be raised.
Note, this only applies if the data parameter is a neo4j.Driver.
Example
In this small example, we import a graph from a Neo4j query result.
from neo4j import GraphDatabase, RoutingControl, Result
from neo4j_viz.gds import from_gds
# Modify this to match your Neo4j instance's URI and credentials
URI = "neo4j://localhost:7687"
auth = ("neo4j", "password")
with GraphDatabase.driver(URI, auth=auth) as driver:
driver.verify_connectivity()
result = driver.execute_query(
"MATCH (n)-[r]->(m) RETURN n,r,m",
database_="neo4j",
routing_=RoutingControl.READ,
result_transformer_=Result.graph,
)
VG = from_neo4j(result)
See the Visualizing Neo4j Graphs tutorial for a more extensive example.