Web application using Flask-RESTPlus

Web application using Flask-RESTPlus

20/05/19   15 minutes read     1788 Naren Allam

flaskpythonbash

In this article, I’ll explain you how to develop a web application using Flask-RESTplus with some APIs and dummy data. We will start our journey toward Flask-RESTplus Web APIs with Python 3.* that performs CRUD (short for Create, Read, Update, and Delete) operations on a sample data.

What is Flask-RESTPlus?

Flask-RESTPlus is an extension for Flask that adds support for quickly building REST APIs. Flask enables exposure of Python functions as APIs. Flask-RESTPlus is an extension to Flask which improves upon its capabilities. It allows us to not only define REST APIs but also brings in Swagger UI for all the APIs.

If you are familiar with Flask, Flask-RESTPlus should be easy to pick up. Flask-RESTPlus encourages best practices with minimal setup. It provides a coherent collection of decorators and tools to describe your API and expose its documentation properly (using Swagger).

SETUP AND INSTALLATION:-

OS Requirements

 ➢ Ubuntu Desktop Linux 16.0.4/18.04

Steps for Installation

 ➢ Open terminal
 ➢ Update ‘sudo ‘ for latest system packages

BASH  Copy
                    
                       $ sudo apt-get update
                    
                  

Make directory

 ➢ Create a directory for storing all the project files in that directory

BASH  Copy
                    
                      $ mkdir dev

$ cd dev
                    
                  

Install pip3 package

 ➢ ‘pip3’ is for installing python packages. To install 'pip3' use the below command

BASH  Copy
                    
                       $ sudo apt-get install python3-pip
                    
                  

Virtual Environment Creation

 ➢ It is a tool to create isolated python environments. It creates a folder which contains all the necessary packages that a python project would need.

BASH  Copy
                    
                       $ sudo apt-get install virtualenv
                    
                  

 ➢ Create a folder with virtual environment

BASH  Copy
                    
                      $ virtualenv venv
                    
                  

 ➢ Here “venv” is the virtual environment folder name , the folder name is of as your wish

Activate virtual environment

BASH  Copy
                    
                      $ source venv/bin/activate


                    
                  

Deactivate virtual environment  ➢ For de-activating the virtual environment, use command:

BASH  Copy
                    
                      $ deactivate
                    
                  

Now start install Flask and Flask-RESTPlus inside the virtual environment which we created. For that run the commands given below

BASH  Copy
                    
                      $ pip install flask
$ pip install flask-restplus
                    
                  

Now we import Flask from flask and from flask_restplus, we import Api to define the app and Resource which is received as a parameter in the various classes defined in the project.

PYTHON  Copy
                    
                      from flask import Flask
from flask_restplus import Api, Resource
                    
                  

Now, define the application as a flask application using the method Flask() which sets the name using __name__. Next, we'll used Api to initialise the application as shown below.

PYTHON  Copy
                    
                      app = Flask(__name__)
api = Api(app, version='1.0', title='SAMPLE API')


                    
                  

1 Comments
Rama

    Very simple explanation and easy to understand. CRUD is very catchy. Great work, Keep it up.