Flask API

Sumanta Mukhopadhyay
3 min readJun 25, 2020

--

Nowadays rest API is everywhere. The advantage of API is to build a logic at a place and use it anywhere to get it consumed.REST is Representational state transfer according to definitions

“A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST, and DELETE data.”

Rest API can be created in many languages starting from C++ (https://github.com/Microsoft/cpprestsdk) and many more like python, java, PHP, Javascript.

Creating API on python gives us extra benefit if we are creating any machine learning application and if we don't have any related to machine learning python should be considered cause its one of the easiest languages and nowadays it's popular though.

So python has a framework for creating rest API or in common terms microservices.

So let's get started for setting up and creating a simple get API and post API with flask.

To get started with making sure we have python 3.5+ or equivalent anaconda distribution is already installed to check the same by the opening terminal. Type

python -V

In my case, I am using windows so I have checked the version on CMD

Create a folder and get into it.

Once the above steps are done create venv(Virtual environment).According to google

A virtual environment (also called a venv) is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python”

Make sure we have venv installed if not

pip3 install --user virtualenv

Once the above is done create a venv by

python -m venv venv

The last venv is the name of my virtual env. Get into the venv by activating the same.

Once venv is activated

venv\Scripts\activate (for windows). venv/bin/activate (for linux)

To install flask in the venv

pip install Flask

Let's start by creating an API that will print hello API on hitting.

Create a python file name it as server.py

Start with by importing libraries

from flask import Flask

Import the Flask class. An instance of this class will be our WSGI application. This will import the flask function intro out python file and we can use it as per definitions.

Next is to create the instance of class just we have imported.

app = Flask(__ name __)

This is needed so that Flask knows where to look for templates, static files, and so on.

Then to create routes for API Uri we have to use route decorator provided by the flask. The function is given a name which is also used to generate URLs for that particular function and returns the message we want to display in the user’s browser.

@app.route('/')
def hello_world():
return 'hello API'

This is the thing that or API will return.

Last step.

if __name__ == "__main__":
app.run()

Run the same by hitting

python server.py

Go to http://127.0.0.1:5000/ to see your project running.

That is what a simple REST API will look like.

More to come ……..
My GitHub:- https://github.com/caeser1996

--

--

Sumanta Mukhopadhyay
Sumanta Mukhopadhyay

No responses yet