cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Simple example using the python driver?

D_C
Node Clone

Is there a simple example of working with the neo4j python driver?
How do I just pass cypher query to the driver to run and return a cursor?

If I'm reading for example this it seems the demo has a class wrapper, with a private member func I pass to the session.write,

session.write_transaction(self._create_and_return_greeting, ...

That then gets called it with a transaction as a first parameter...

def _create_and_return_greeting(tx, message):

that in turn runs the cypher

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

This seems 10X more complicated than it needs to be.

I did just try a simpler:

def raw_query(query, **kwargs):
    neodriver = neo_connect()  # cached dbconn
    with neodriver.session() as session:
        try:
            result = session.run(query, **kwargs)
            return result.data()

But this results in a socket error on the query, probably because the session goes out of scope?

[dfcx/__init__] ERROR | Underlying socket connection gone (_ssl.c:2396)

[dfcx/__init__] ERROR | Failed to write data to connection IPv4Address(('neo4j-core-8afc8558-3.production-orch-0042.neo4j.io', 7687)) (IPv4Address(('34.82.120.138', 7687)))

Also I can't return a cursor/iterator, just the data()
When the session goes out of scope, the query result seems to die with it.

If I manually open and close a session, then I'd have the same problems?

Python must be the most popular language this DB is used with, does everyone use a different driver?
Py2neo seems cute, but completely lacking in ORM wrapper function for most of the cypher language features, so you have to drop down to raw cypher anyway. And I'm not sure it supports **kwargs argument interpolation in the same way.

I guess that $300M raise got blown on marketing.

Slightly longer version trying to get a working DB wrapper:

def neo_connect() -> Union[neo4j.BoltDriver, neo4j.Neo4jDriver]:

    global raw_driver
    if raw_driver:
        # print('reuse driver')
        return raw_driver

    neoconfig = NEOCONFIG
    raw_driver = neo4j.GraphDatabase.driver(
        neoconfig['url'], auth=(
            neoconfig['user'], neoconfig['pass']))
    if raw_driver is None:
        raise BaseException("cannot connect to neo4j")
    else:
        return raw_driver


def raw_query(query, **kwargs):
    # just get data, no cursor
    neodriver = neo_connect()
    session = neodriver.session()
    # logging.info('neoquery %s', query)
    # with neodriver.session() as session:
    try:
        result = session.run(query, **kwargs)
        data = result.data()
        return data

    except neo4j.exceptions.CypherSyntaxError as err:
        logging.error('neo error %s', err)
        logging.error('failed query: %s', query)
        raise err
    # finally:
    #     logging.info('close session')
    #     session.close()
6 REPLIES 6

I'm not a Python user, but as far as I know, Py2Neo offers OGM support: 4. py2neo.ogm – Object-Graph Mapping — The Py2neo v4 Handbook.
For a simple example with the raw Python driver, you can find one here: GitHub - neo4j-examples/movies-python-bolt: Neo4j Movies Example application with Flask backend usin...

EDIT: please refrain from duplicating posts across different channels. We're monitoring all of them and this needlessly fragments our effort to answer questions.

D_C
Node Clone

as mentioned in the post above, I've tried py2neo, but hit a roadblock almost immediately, so it doesn't seem like a mature option.

re crossposting: I didn't get much response here on previous questions, so I'm trying to find an active community for this DB. StackOverflow seems quiet too. Is there a slack/discord with more activity?

Py2neo has been developed for 10+ years, so calling it immature is quite a harsh statement, to say the least.

To be fair, most of that was as an unofficial side project, and a great effort by the guy working on it.

We tried to provide a simpler example for the sandboxes:

D_C
Node Clone

this looks useful, I might try this.
I have scoured the docs over the weekend and never seen this...