Skip to content

WIP: Allow Driver as argument to from_neo4j #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions python-wrapper/src/neo4j_viz/neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
from typing import Optional, Union

import neo4j.graph
from neo4j import Result
from neo4j import Driver, Result, RoutingControl

from neo4j_viz.node import Node
from neo4j_viz.relationship import Relationship
from neo4j_viz.visualization_graph import VisualizationGraph


def from_neo4j(
result: Union[neo4j.graph.Graph, Result],
result: Union[neo4j.graph.Graph, Result, Driver],
size_property: Optional[str] = None,
node_caption: Optional[str] = "labels",
relationship_caption: Optional[str] = "type",
node_radius_min_max: Optional[tuple[float, float]] = (3, 60),
row_limit: int = 10_000,
) -> VisualizationGraph:
"""
Create a VisualizationGraph from a Neo4j Graph or Neo4j Result object.
Expand Down Expand Up @@ -44,8 +45,16 @@ def from_neo4j(
graph = result.graph()
elif isinstance(result, neo4j.graph.Graph):
graph = result
elif isinstance(result, Driver):
graph = result.execute_query(
f"MATCH (n)-[r]->(m) RETURN n,r,m LIMIT {row_limit}",
routing_=RoutingControl.READ,
result_transformer_=Result.graph,
)
Comment on lines +49 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think if we allow a driver here, we should also allow to specify a query here instead of the row_limit.
Otherwise we end up with building a Cypher query builder

Copy link
Collaborator Author

@adamnsch adamnsch May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I agree with that. If they want to do their own query they can do it first, and then just provide the Result. The idea of this is to make it so that they don't even have to make a query - that's the added value

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay if thats the main part, how about just passing the url + auth instead and we construct the client as well?

I think its always a bit to remember how to construct the driver (>0s)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a cool idea! I'll try it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I don't like is that it requires more parameters

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can have a dataclass to combine url + auth. gds has DbmsConnectionInfo now.

else:
raise ValueError(f"Invalid input type `{type(result)}`. Expected `neo4j.Graph` or `neo4j.Result`")
raise ValueError(
f"Invalid input type `{type(result)}`. Expected `neo4j.Graph`, `neo4j.Result` or `neo4j.Driver`"
)

all_node_field_aliases = Node.all_validation_aliases()
all_rel_field_aliases = Relationship.all_validation_aliases()
Expand Down