Flask, a popular Python web framework, has many tutorials available online which use an SQL database to store information about the website’s users and their activities.

While SQL is a great tool for storing information such as usernames and passwords, it is not so great at allowing you to find connections among your users for the purposes of enhancing your website’s social experience.

The quickstart Flask tutorial builds a microblog application using SQLite. 

In my tutorial, I walk through an expanded, Neo4j-powered version of this microblog application that uses py2neo, one of Neo4j’s Python drivers, to build social aspects into the application. This includes recommending similar users to the logged-in user, along with displaying similarities between two users when one user visits another user’s profile.

My microblog application consists of Users, Posts, and Tags modeled in Neo4j:


With this graph model, it is easy to ask questions such as:

“What are the top tags of posts that I’ve liked?”

MATCH (me:User)-[:LIKED]->(post:Post)<-[:TAGGED]-(tag:Tag)
WHERE me.username = 'nicole'
RETURN tag.name, COUNT(*) AS count

“Which user is most similar to me based on tags we’ve both posted about?”

MATCH (me:User)-[:PUBLISHED]->(:Post)<-[:TAGGED]-(tag:Tag), 
WHERE me.username = 'nicole' AND me <> other
WITH other,
      COLLECT(DISTINCT tag.name) AS tags,
    COUNT(DISTINCT tag) AS len
ORDER BY len DESC LIMIT 3 RETURN other.username AS similar_user, tags

Links to the full walkthrough of the application and the complete code are below.

Watch the Webinar:

Want to learn more about graph databases? Click below to get your free copy of O’Reilly’s Graph Databases ebook and discover how to use graph technologies for your application today.

Download My Copy



About the Author

Nicole White , Data Scientist

Nicole White Image

Nicole White grew up in Kansas City, Missouri and then spent four years at LSU in Baton Rouge, Louisiana where she got a degree in economics with a minor in mathematics. She then went to the University of Texas at Austin where she got her masters degree in analytics, and it was during this time that she found Neo4j and began playing around with it. When she’s not graphing all the things, she spends her time playing card games and board games.


Avanti Patil says:

Hi Nicole

Excellent work with the demo site.
It is really useful. I am working on the similar project.
If you want I can contribute to your project to add some functionalities.

Avanti Patil

Antonina says:

I tried to follow up your video but I have a problem. Have in mind that I’m new to all of this “tribute” (Flask-Neo4j-Python). I ‘ve downloaded the herokuapp byt github and entered the terminal
commands export NEO4J_USERNAME = ‘username’, NEO4J_PASSWORD = ‘password’ and ran it it was perfect. Then i tried to make the steps with you in the video. I ran again the command ‘python run.py’ but I
have a traceback and the link that was opened the first time localhost://5000 isn’t available now…
What I’m doing wrong? These are the errors :
~/Desktop/flasktest/nicolewhite-neo4j-flask-27476c5$ python run.py
Traceback (most recent call last):
File “run.py”, line 1, in
from blog import app
File “/home/antonina/Desktop/flasktest/nicolewhite-neo4j-flask-27476c5/blog/__init__.py”,
line 4, in
graph.schema.create_uniqueness_constraint(“User”, “username”)
File “/usr/local/lib/python2.7/dist-packages/py2neo/database/__init__.py”,
line 775, in create_uniqueness_constraint
{“property_keys”: [property_key]})
File “/usr/local/lib/python2.7/dist-packages/py2neo/database/http.py”,
line 212, in post
raise_from(GraphError(message, **content), error)
File “/usr/local/lib/python2.7/dist-packages/py2neo/util.py”, line
122, in raise_from
raise exception
py2neo.database.status.ConstraintViolationException: Constraint
already exists: CONSTRAINT ON ( user:User ) ASSERT user.username IS

Thanks for this great article, I have shared it
on Twitter.

Srinivas says:

Thank You for this article, it is very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *