Rest-api-with-CRUD-operation-using-Flask

Introduction:

Hi, reader, I hope you are doing great. Today I came up with an Innovative article for creating rest api in Flask framework. We have already discussed Django web development framework. In this Article I have Created an api with CRUD operation. Basically CRUD Stands for Create-Read-Update-Delete in our data base. I have used SQLite database because of it is portable.

What is Flask and Why Flask ?

Flask is one of the most popular python's web development framework. Most developer prefer flask because its simplicity and its light weight. In a nutshell it is easy to work with flask as Compared with Django.

File Structure of this Project:

Usually flask app have only one file but I have split this project in 3 mains file just because of easiness.

Screenshot from 2020-05-17 21-24-22

Lets talk about these files briefly :

data.sqlite: As I already discussed I have used Sqlite database for this rest api because it is portable and it remove the dependency to setup new database. This data.sqlite file is our database file which is connected using SQLAlchemy.

models.py: This file contains our database table details, In this file we can change our table structures like add new fields , remove existing one and add other validation properties.

settings.py: This is our settings file where we can define configurations of our application like define our database setting and debug settings. In this file I have defined two paths for linux as well as for windows os. Currently I have commented windows path and used linux path. If you are using window just uncomment it.

myapp.py: This is core file of our api which will handle all the CRUD operation on specific url with specified Http request method. I have created methods for handling Http request and performing CRUD operations.

requirement.txt: This is file contains dependencies information to run this project.

Requirements:

what you will learn from this tutorial:

  • How to build rest Api in Flask
  • Create database with Flask using SQLAlchemy
  • Handling CRUD Operations

how to run this code:

  • clone or download it from here
  • install requirements.txt file
  • pip install -r requirements.txt
  • run this command to execute this code
    python myapp.py
    Screenshot from 2020-05-17 21-47-20

    When it runs your temrminal/cmd will be like above image

Output:

Screenshot from 2020-05-14 14-34-02

  • For creating new user with post request using postman

Screenshot from 2020-05-14 14-31-31

  • Update user details using Put request using Postman
  • Screenshot from 2020-05-14 14-32-13
  • Delete user using delete user using postman
  • Screenshot from 2020-05-14 14-33-14