33from typing import Optional , Union
44
55import neo4j .graph
6- from neo4j import Result
6+ from neo4j import Driver , Result , RoutingControl
77from pydantic import BaseModel , ValidationError
88
99from neo4j_viz .node import Node
@@ -20,14 +20,15 @@ def _parse_validation_error(e: ValidationError, entity_type: type[BaseModel]) ->
2020
2121
2222def from_neo4j (
23- result : Union [neo4j .graph .Graph , Result ],
23+ result : Union [neo4j .graph .Graph , Result , Driver ],
2424 size_property : Optional [str ] = None ,
2525 node_caption : Optional [str ] = "labels" ,
2626 relationship_caption : Optional [str ] = "type" ,
2727 node_radius_min_max : Optional [tuple [float , float ]] = (3 , 60 ),
28+ row_limit : int = 10_000 ,
2829) -> VisualizationGraph :
2930 """
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 .
3132
3233 All node and relationship properties will be included in the visualization graph.
3334 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(
3637
3738 Parameters
3839 ----------
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.
4143 size_property : str, optional
4244 Property to use for node size, by default None.
4345 node_caption : str, optional
@@ -47,14 +49,25 @@ def from_neo4j(
4749 node_radius_min_max : tuple[float, float], optional
4850 Minimum and maximum node radius, by default (3, 60).
4951 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.
5055 """
5156
5257 if isinstance (result , Result ):
5358 graph = result .graph ()
5459 elif isinstance (result , neo4j .graph .Graph ):
5560 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+ )
5667 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+ )
5871
5972 all_node_field_aliases = Node .all_validation_aliases ()
6073 all_rel_field_aliases = Relationship .all_validation_aliases ()
0 commit comments