Introduction to Web Development using Flask:

Flask: It is an API which helps in building up of web-applications. Armin Ronacher developed Flask. It has a more explicit framework than Django. It is easier to learn as it has less base code for the implementation of a simple web-application. Web application framework or web framework is a collection of modules and libraries. It helps the developer to write applications without writing low-level codes like protocols, thread management, etc. Flask has its base on WSGI (Web Server Gateway Interface) toolkit and Jinja2 template engine. Though python has very vast useses in data science but we cannot neglect its web applications as well.

Getting started:

We require Python 2.6 or above for installing Flask. First, we need to import Flask from the flask package from any Python IDE. For testing the installation, check the below code.

python for data science

The ‘\’ URL is bound by the hello() function. When we open the home page of a web server in a browser, the output of the function is render accordingly.

We will start the Flask application by calling the run() function. If there is any change in the code, we need to restart the method manually. For overcoming this, we have enable the debug support for tracking of any error.

python for data science


The web framework nowadays provides routing technique for users to remember the URLs. It will be handy when we want to access the web page directly without going to the homepage. The route() decorator is there for this and binding the URL to a function.

python for data science

We can use the add_url_rule() function of an application object for binding URL with the function.

Using variables in Flask:

We use variables in Flask for building a URL dynamically by adding variables part to the rule parameter. The variable part is pass as keyword argument.

Apart from string variable part, we can also use datatypes like float, int, and path. The URL rules follow by Flak is base on Werkzeug’s routing module. It will ensure URLs form are unique and are also base on precedents laid down by Apache.

python for data science

Building URL in Flask:

We build a dynamic URL for a certain function using url_for() function. The name of the function is the first argument and one or more keywords as other arguments.

python for data science

python for data science

Handling static files:

In a web application, there is always the requirement of a static file like javascript or CSS for rendering the display of web page in the browser. Web servers are configure for setting them, but during development these files will be serve as static folder in our package or next to the module.

Below we have an example in javascript,

python for data science

The object request of data from a client’s web page is sent to the server as a global request object. We then process it using the flask module. It consists of attributes such as form, args, cookies, files, and method.


It is a form of text file stored on the client’s computer. Its purpose is to remember and track data according to the client’s usage for improving the website according to the user experience and the static of the web page.

Cookie’s attributes are present in the request object. It is dictionary object of all cookie variables and the values corresponding to them. It also has an expiry time of itself. We set cookie on response object in Flask.

Sessions in Flask:

In a session, the data is stored on a server. It is defined as a time interval in which the client logs into a server until the user logs out. Temporary folder on the server is there for holding the data in between them. Each user will get a unique session ID. We will use a SECRET_KEY for storing the encrypted data on the cookie.


Other important Flask functions:

redirect(): It is there for returning the response of an object and then redirects the user to another target location with a specified status code.

abort: We use it for handling errors in code.

Syntax:  Flask.abort(code)

Code parameter will take the following values for handling the error:

File uploading in Flask:

It is very easy to upload files in Flask. It will need an HTML form with encryption type and URL handler which will fetch the files and save the objects to the location specified by the user. The files are temporarily stored on the server and after that on the specified location. The HTML syntax for handling the uploading of the URL is as follows:

The Python code for Flask is as follows which has some applications in data science as well:

Sending the form data to the HTML file of server:

In HTML we use a form for collecting information of the required entries and then forwarded and stored on the server. We can raise a request to read or modify the form. The Flask uses the URL rule for providing this facility.

Message flashing:

It is a pop-up or dialog box which appears on web page or like alert in JavaScript, which is there for informing the users. We can perform this in Flask by using the method flash(). It will pass the message to next template.

In the syntax message is the actual text which will be displayed and category is optional for rendering any error or info.


So, to learn more about python and its uses in python for data science, you can check this and this as well.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.