Good tutorials on py2neo?

I'm getting back into neo4j and the py2neo ORM seems the nicest way to work with the data, and the drivers had a lot of updates since pandemic started.

But the docs are still sorely lacking. I was wondering if others can share links or code snippets that demo some of the features working with py2neo?

for example:

Hi @DC1, the documentation for py2neo are very detailed.

my code for neo4j pivot -> http://www.dominickumar.com/blog/neo4j-pivot-functionality/

Can you maybe give an idea of what you are trying to do or what errors you are getting from your script?

Personally (and just a matter of preference), I use the official Python neo4j package over py2neo and find that it does all that I need it to, plus I find the docs to be pretty helpful. But YMMV.

Hi @cj20011, as your request for a code snippet , I shared my code where I used py2neo. I am not getting any errors from my script.

I am also currently moving towards Neo4j python, since py2neo doesn't support Clustering. We have Cluster setup for Production. Hope this works for you. ..

thanks for your replies all
@dominicvivek06 nice tutorials and blog!

@cj20011 I'm trying to build a knowledge graph from reddit posts. esp liked the one on demo data, tho not so python related.

It's just taking me time to find how to do things like "don't create duplicate nodes" or if there's a way to create a node and relation at the same time. Probably basic for someone who knows neo4j but quite slow and hard to find with py2neo.

I would prefer long term to use an ORM, rather than writing raw cypher queries. I find it a more natural way to think about my data models and add features as I need them to the right classes. I think py2neo also allows you to drop down into run cypher queries so it's the best of both worlds? Is there anything significant missing that is in the official python driver?

@DC1 - This is 100% correct, and this is how the application should be built.
`

I would prefer long term to use an ORM, rather than writing raw cypher queries. I find it a more natural way to think about my data models and add features as I need them to the right classes.

`

py2neo doesn't have close() method, but official neo4j python does.
py2neo is not cluster-aware, but official neo4j python is.

One of the thread I replied to

I've started a series of blog posts on the Neo4j python driver and will eventually extend into Py2Neo. Not sure if what I have is complete enough to answer your questions but stay tuned as more is coming.

@dominicvivek06 I think you are right. The py2neo handbook is very detailed. BUT the way it is written and the types of examples are not very helpful. I have been using Neo4j for about two years and I have had to piece together most of what i learned through videos and trial and error. Even referencing stuff from Nicole White is old and outdated. I sent a message to Nigel Small, author of py2neo, to create more examples that are beyond a dumb movies db and not python console. I want to see examples of applications with the use of these class's, cls methods, static methods, functions..... Nicole was off to a great start by using a Flask app to demo, but that is out of date.

1 Like

@DC1 I wrote a newer version of the Flask app demo. check out GitHub - josh-thurston/FlaskNeo4jStarter: Flask with Neo4j Starter that includes User Auth and Session Management.

1 Like

There has been an update to the Flask tutorial here too: Getting Started with Neo4j and Flask - Developer Guides.

I'm in a similar position to original poster, new to Flask, Python, and Neo4j.

After some pondering I think I prefer the py2neo driver, but have the same documentation issues, the documentation tells me precisely how to do something, but not why and when I would want to do it.

The starter app you've done there looks great, although I've just written much the same as part of learning it all. The biggest problem today was older documentation out there, like Nicole's, call methods line "find_one" that are gone now in py2neo.

I've understood that to avoid the duplicate nodes, I have to merge (hopefully OP has got that now). But then when I work on a "Node" in py2neo, I find that existing properties are updated in the database immediately, but I have to "push" for new properties.

For example if "pw_hash" is present and "pw_changed" isn't....

matcher = NodeMatcher(graph)
thisUser = matcher.match("Person", name=name).first()
if not thisUser:
user = Node('Person', name=name, pw_hash=pw_hash)
graph.create(user)
else:
thisUser["pw_hash"] = pw_hash
thisUser["pw_changed"] = neotime.DateTime.now()
graph.push(thisUser)

The "push" is required for the update to include pw_changed. But I'm not even sure this is the right, or optimal approach. Should I have done "thisUser.update" or "thisUser.properties" ? Without some meta description of these options it is hard to know.

Part of the issue is that it is deliberately unrestrictive. I can do anything by pushing the Cypher, or playing with nodes and relationships directly, or matching and updating parts of the graph locally, and pushing them back, or use the OGM.

I'm expecting my model to evolve fairly rapidly. So that as I add features to the application, each new feature will need additional properties on existing nodes. I was planning to use the Node and Relationship objects from py2neo, and have the code update the models, but not sure if an OGM approach makes more sense. Or how hard it would be to maintain those classes, it feels that if the additional features require properties and relationships, they should add them. But it was only reading the py2neo documentation I (re-)discovered nodes could have more than one label (I think I missed something in the earlier training and doocumentation somewhere, as the docs are clear "zero" to "many" labels). When to use multiple labels, and temporary labels is something I clearly need to learn. If I extend a base object, say I have a "User" who is also an "Employee" does it make sense to have that as a label as well perhaps as a relationship?

Another concern is that a lot of the example "Movie" apps looks fine and understandable, but I wondered what "operational" code looks like in this space. e.g. I see a lot of database updates with no explicit error handling, no logging etc. I'm assuming "real" code in this space often has more checking?

That said I'm still prototyping things, and switching between drivers and trying out little scripts, whatever I settle on I'll abstract away as much as I can so changing things shouldn't be too hard.