Prerequisites: Basic python knowledge.


Flask is a web application framework written with Python language. Applications written on Flask can be as simple as a single .py file or it can have multiple modules with different functionalities.

In short a Flask application is an instance of the Flask class. For small applications we create a Flask instance globally. For large applications we instantiate the same in a function called as application factory( inside __init__.py). Any configuration or setup requirements are also added to this application factory.

While working on a project SQR to Python we had to integrate Flask to deliver API responses to a Django project.

However, as an example for this tutorial we will process some text using the TextBlob library. We will have some endpoints which will receive a request with text payload, and after processing , will respond with the processed text.

To start with, lets create a flask directory to save your files into.

Now we will cd into this directory and create a api.py file.

 

Lets start with a basic root(‘/’) route. Copy and paste the below mentioned code into the api.py file. This will expose a route “/” which can be accessed as a get request from the browser.

We can now start the flask server using the command below

This will start the server at port 5000. If you open your browser at url – http://127.0.0.1:5000/ you should be able to see “Hello World” as the output.

Now lets write a method for an API endpoint to accept a text and return a text with correct spellings (in case if there are any spelling mistakes in your text )

Now as in the screenshot above, you have to send the request to you endpoint url and you should get a corrected text as the response.Like here in this example, we are sending the request “Havv a nice day!” and we get “Have a nice day ” in response.

Now lets create endpoints for noun phrase extraction ,detection of language and translation to other languages.

So this is the final code we have after writing all the methods above.

You can add these to your app.py file. Note: you will need to install some additional packages for the processing like “python -m textblob.download_corpora”. All these endpoints can be requested using a REST client like Postman or Advanced Rest Client.

All the APIs will return processed text data in JSON format.
We can consume these APIs with some front end framework to display it to the user.

You can create really big projects with flask with complete authentication and authorisation.

You can explore more on Flask and TextBlob. The official documentation is really helpful.
Getting started with Flask is extremely easy.