Simple Introduction to Flask


Most of you must have been familiar with Python which is an interpreted language, very easy to learn, and powerful. Python can be used on a server to create web applications. To make our work even easier, there is a framework based on Python called Flask.

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.

In general, WSGI is a Python standard.

cited source: https://wsgi.readthedocs.io/

Flask has become one of the most popular Python web application frameworks. If you are interested in web development, Flask will be another important framework you need to learn. Now, I will lay out some facts about Flask.

When you start using Flask, the first thing you need to check is the version of Python on your computer. Flask is compatible with Python 3.7 and newer.

Unlike node.js, npm can help you manage the packages. If you would have multiple Python projects, you really need to set up a virtual environment to manage the dependencies for your project. So the different versions of Python libraries wouldn’t intervene with each other. The reason is that newer versions of libraries for one project can break compatibility in another project. The virtual environment makes sure the installation of packages is project-wide not global-wide.

Before we start building URLs, let’s learn one more thing: RESTful API. API s is the Application Programming Interface. The web server you built is an API. REST stands for REpresentational State Transfer and is a software architecture style that defines a pattern for client and server communications over a network. A REST web service is any web service that adheres to REST architecture constraints. These web services expose their data to the outside world through an API. REST APIs provide access to web service data through public web URLs.

Web applications use different HTTP methods when accessing URLs. While there are many HTTP methods, the five methods listed below are the most commonly used with REST APIs:

HTTP methodDescription
GETRetrieve an existing resource.
POSTCreate a new resource.
PUTUpdate an existing resource.
PATCHPartially update an existing resource.
DELETEDelete a resource.

A REST API exposes a set of public URLs that client applications use to access the resources of a web service. These URLs, in the context of an API, are called endpoints. These endpoints represent different resources. The combination of HTTP methods and endpoints supports the CRUD functionality. CRUD comes from “create, read, update, and delete“. For example, online stores manage their customers.

HTTP methodAPI endpointDescription
GET/customersGet a list of customers.
GET/customers/<customer_id>Get a single customer.
POST/customersCreate a new customer.
PUT/customers/<customer_id>Update a customer.
PATCH/customers/<customer_id>Partially update a customer.
DELETE/customers/<customer_id>Delete a customer.

Flask is a great tool and easy to start with. However, bad design will lead your web service hard to use. To make your service have the ability to scale up to complex applications, there are more things to consider. Hopefully, this post can give you some insights when building a RESTful web service.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *