3
3
from typing import Optional , Union
4
4
5
5
import neo4j .graph
6
- from neo4j import Result
7
6
from pydantic import BaseModel , ValidationError
7
+ from neo4j import Driver , Result , RoutingControl
8
8
9
9
from neo4j_viz .node import Node
10
10
from neo4j_viz .relationship import Relationship
@@ -20,14 +20,15 @@ def _parse_validation_error(e: ValidationError, entity_type: type[BaseModel]) ->
20
20
21
21
22
22
def from_neo4j (
23
- result : Union [neo4j .graph .Graph , Result ],
23
+ result : Union [neo4j .graph .Graph , Result , Driver ],
24
24
size_property : Optional [str ] = None ,
25
25
node_caption : Optional [str ] = "labels" ,
26
26
relationship_caption : Optional [str ] = "type" ,
27
27
node_radius_min_max : Optional [tuple [float , float ]] = (3 , 60 ),
28
+ row_limit : int = 10_000 ,
28
29
) -> VisualizationGraph :
29
30
"""
30
- Create a VisualizationGraph from a Neo4j Graph or Neo4j Result object.
31
+ Create a VisualizationGraph from a Neo4j Graph, Neo4j Result object or Neo4j Driver .
31
32
32
33
All node and relationship properties will be included in the visualization graph.
33
34
If the properties are named as the fields of the `Node` or `Relationship` classes, they will be included as
@@ -36,8 +37,9 @@ def from_neo4j(
36
37
37
38
Parameters
38
39
----------
39
- result : Union[neo4j.graph.Graph, Result]
40
- Query result either in shape of a Graph or result.
40
+ result : Union[neo4j.graph.Graph, neo4j.Result, neo4j.Driver]
41
+ Either a query result either in the shape of a `neo4j.graph.Graph` or `neo4j.Result`, or a `neo4j.Driver` in
42
+ which case a simple default query will be executed to retrieve the graph data.
41
43
size_property : str, optional
42
44
Property to use for node size, by default None.
43
45
node_caption : str, optional
@@ -47,14 +49,25 @@ def from_neo4j(
47
49
node_radius_min_max : tuple[float, float], optional
48
50
Minimum and maximum node radius, by default (3, 60).
49
51
To avoid tiny or huge nodes in the visualization, the node sizes are scaled to fit in the given range.
52
+ row_limit : int, optional
53
+ Maximum number of rows to return from the query, by default 10_000.
54
+ This is only used if a `neo4j.Driver` is passed as `result` argument, otherwise the limit is ignored.
50
55
"""
51
56
52
57
if isinstance (result , Result ):
53
58
graph = result .graph ()
54
59
elif isinstance (result , neo4j .graph .Graph ):
55
60
graph = result
61
+ elif isinstance (result , Driver ):
62
+ graph = result .execute_query (
63
+ f"MATCH (n)-[r]->(m) RETURN n,r,m LIMIT { row_limit } " ,
64
+ routing_ = RoutingControl .READ ,
65
+ result_transformer_ = Result .graph ,
66
+ )
56
67
else :
57
- raise ValueError (f"Invalid input type `{ type (result )} `. Expected `neo4j.Graph` or `neo4j.Result`" )
68
+ raise ValueError (
69
+ f"Invalid input type `{ type (result )} `. Expected `neo4j.Graph`, `neo4j.Result` or `neo4j.Driver`"
70
+ )
58
71
59
72
all_node_field_aliases = Node .all_validation_aliases ()
60
73
all_rel_field_aliases = Relationship .all_validation_aliases ()
0 commit comments