Implementing Flask Login with Neo4j

Taiwo O. Adetiloye
3 min readDec 27, 2019

--

Fig 1. Source: https://wallpaperaccess.com/. Retrieved Dec. 27, 2019

A very good reason to want to code with Python is the richness in various open-source libraries for quick application development. One such library is Flask Login that provides user session management for Flask with handlers for logging in and out, as well as remembering users’ sessions over extended periods of time. It uses a Login Manager class to facilitate integration with a web application. Most of these applications rely on a relational database(RDBMS) using standard SQL such as MySQL, SQLite, PostgreSQL etc. to store the user login and registration data e.g name, encrypted passport, address, website. A good tutorial on Flask Login can be found here and it would be re-adapted for our use-case.

Unlike RDBMS, Neo4j is a popular open-source graph database that uses Cypher Query Language (CQL) without any foreign-key, primary-key relationships. It goes without saying that the conciseness of CQL does the job. For more details on Neo4j vs RDMS refer to Concepts: Relational to Graph.

So, how does one implement Flask Login with Neo4j Database?
White(2015) developed an excellent use-case of a simple microblog application of Flask and Neo4j while replacing SQL database with Neo4j. In the application, Neo4J CQL is used to create nodes and relationships for USERS, TAG and POST with unique constraints and to implement queries that show the recommendation of similar users to the logged-in user, along with displaying similarities between two users when one user visits another user’s profile.

For this flasklogin-neo4j framework, the primary focus is integrating Flask Login with the Neo4j graph database with a simple dashboard to log into. Py2neo serves as a client library and toolkit for working with Neo4j from Python applications. Fig 2. illustrates the main structure.

Fig. 2. Structure of flasklogin-neo4j framework

The following are the key python codes:

__init__.py

flasks_py2neo.py

model.py

run.py

Full code on GITHUB.

The application should run successfully provided that the Neo4j server has been started. This can be done by downloading the community edition and starting the server using the Windows command:

./bin/neo4j console

And of course, upon registration, the user can view the dashboard.

In the Neo4j browser( i.e. localhost:7474/browser), the following CQL should help find a registered user:

match (n:User) where n.name=<username> return n

Neo4j offers a plethora of queries that data and software engineers can use to develop cool dashboards. Please, study Getting Started with Data Analysis using Neo4j [Community Post] and you can get this Neo4j ebook.

For technical support, feel free to reach me via my coordinate.

References

Birchard, T.(2019), “Using Flask-Login to Handle User Accounts”. Retrieved from https://hackersandslackers.com/flask-login-user-authentication, and GITHUB:https://github.com/toddbirchard/flasklogin-tutorial

White, N.(2015), “Building a Python web application using Flask and Neo4j” OSCON, Portland, Oregon, Accessed at https://conferences.oreilly.com/oscon/open-source-2015/public/schedule/detail/42146

About the author

Taiwo O. Adetiloye has a PhD in Information and Systems Engineering, with over a decade of software research and development experience. He is interests lie in software development with large-scale data processing and analytics using AI and ML frameworks like Spark, Keras, Tensorflow, Pytorch, and MxNet. His favourite programming languages are Golang, Python, Java and Scala.

He is constantly developing himself as an information technologist working and acquiring new skill sets to help in his career growth. He is a highly motivated individual, a good communicator and a great team player with a passion for creativity and a drive for excellence.

Other articles by the author

  1. Setting up Jupyter Notebook on Visual Studio Code for Data Analytics
  2. Analyzing the Amazon Product Data Set using SparkMLlib LogisticRegression Classification Model

3. Imagine a world without guns

4. Searching within

5. Finding a Holistic Approach to Students’ Academic and Social Wellbeing

6. COVID-19: Cherish, Seek and Pursue Peace

Useful links about the author

Website
Github
LinkedIn

--

--

Responses (1)