Thursday, October 22, 2020

How to use neo4j with Python

Neo4j can be installed on any system and then accessed via its binary and HTTP APIs.

You can use the official binary driver for Python (neo4j-python-driver) or connect via HTTP with any of our community drivers.


Neo4j Python Driver

The Neo4j Python driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to Python.

pip install neo4j

class HelloWorldExample:


    def __init__(self, uri, user, password):

        self.driver = GraphDatabase.driver(uri, auth=(user, password))


    def close(self):

        self.driver.close()


    def print_greeting(self, message):

        with self.driver.session() as session:

            greeting = session.write_transaction(self._create_and_return_greeting, message)

            print(greeting)


    @staticmethod

    def _create_and_return_greeting(tx, message):

        result = tx.run("CREATE (a:Greeting) "

                        "SET a.message = $message "

                        "RETURN a.message + ', from node ' + id(a)", message=message)

        return result.single()[0]



if __name__ == "__main__":

    greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "password")

    greeter.print_greeting("hello, world")

    greeter.close()



Py2neo


Py2neo is a client library and comprehensive toolkit for working with Neo4j from within Python applications and from the command line. It has been carefully designed to be easy and intuitive to use




References:

https://neo4j.com/developer/python/

No comments:

Post a Comment