Building a Python Web Application Using Flask and Neo4j
Join the DZone community and get the full member experience.Join For Free
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 order by count desc“which user is most similar to me based on tags we’ve both posted about?”
match (me:user)-[:published]->(:post)<-[:tagged]-(tag:tag), (other:user)-[:published]->(:post)<-[:tagged]-(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
Published at DZone with permission of Andreas Kollegger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.