Show Graph results using Neo4j Python driver

57 Views Asked by At

I'm currently working with knowledge graphs with Neo4j using Python Driver. I'm already connected to localhost

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "neo4j")
   
   
with GraphDatabase.driver(URI, auth=AUTH) as driver:
    driver.verify_connectivity()

driver: Driver = GraphDatabase.driver(URI, auth=AUTH)

Now I would like to display the results as a graph of some queries like the following:

query = 'MATCH (n)-[r]->(c)  where r.filePath = "../data/mydata.json" RETURN *'

driver.execute_query(query)

From what I understood, python driver does not interact directly with neo4j browser. So which is the best way to show the resulting Graph?

1

There are 1 best solutions below

1
Robsdedude On BEST ANSWER

What do you expect the code to stream? The Cypher query doesn't have a RETURN clause.

Try to inspect the summary of the EagerResult object returned from execute_query. Here's an example

from neo4j import GraphDatabase


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "neo4j")


with GraphDatabase.driver(URI, auth=AUTH) as driver:
    driver.verify_connectivity()
    result = driver.execute_query(
        "CREATE (charlie:Person:Actor {name: 'Charlie Sheen'})"
        "-[:ACTED_IN {role: 'Bud Fox'}]->"
        "(wallStreet:Movie {title: 'Wall Street'})"
        "<-[:DIRECTED]-"
        "(oliver:Person:Director {name: 'Oliver Stone'})"
    )
    print(result.summary.counters)

Which gives me

{'_contains_updates': True, 'labels_added': 5, 'relationships_created': 2, 'nodes_created': 3, 'properties_set': 4}

So it's clearly doing something.

If this doesn't work, it can be helpful to enable the driver's debug logging to see what it's up to. There's a section in the API docs that shows how to do that: https://neo4j.com/docs/api/python-driver/current/api.html#logging

The simplest form looks like this

from neo4j.debug import watch

watch("neo4j")
# from now on, DEBUG logging to stderr is enabled in the driver