Flask-The Web Application Framework.

Chetanya-Patil
3 min readMar 25, 2020

PART — (1)

Flask is a web application framework written in python.

What is Web Framework?
Web application Framework represents a collection of libraries and modules that help a web developer to write applications without having tense of protocols, thread management etc.

What is Flask?
Flask is a web application framework written in python. it is developed by Armin Ronacher,who leads an international group of python enthusiasts named pocco. Flak is based on the Werkzeug WSGI toolkit and jinja2 template engine.
Both are Pocco Projects.

WSGI
Web server Gateway Interface(WSGI) has been adopted as a standard for python web application development.

Werkzeug
It is a WSGI toolkit, which implements requests,response objects,and other utility functions.This enables building a web framework on top of it. The Flask framework uses werkzeug as one of its bases.

Jinja2
Jinja2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.

Flask is often referred to as a micro framework. It aims to keep the core of an application simple yet extensible. Flask does not have built-in abstraction layer for database handling, nor does it have form a validation support. Instead, Flask supports the extensions to add such functionality to the application.

FLASK — — — -ENVIRONMENT

Python 2.6 or higher is usually required for installation of flask. Although flask and its dependencies work well with python 3(python 3.3 onwords), many flask extensions do not support it properly. Hence,it is recommended that flask should be installed on python 2.7.

Install virtualenv for development environment
Virtualenv is a virtual python environment builder.it helps a user to create multiple python environment side-by-side. Thereby, it can avoid compatibility issues between the different versions of the libraries .

The following command installs virtualenv

pip install virtualenv

This command needs administrator privileges. Add sudo before pip on Linux/Max OS. If you are on windows,log in as Administrator. On Ubuntu virtualenv may be installed using its package manager.

Sudo apt-get install virtualenv

Once installed, new virtual environment is created in a folder.

mkdir newproj
cd newproj
virtualenv venv

To activate corresponding environment, on Linux/OS X, use the following —

venv/bin/activate

On Windows, following can be used

venv\scripts\activate

We are now ready to install Flask in this environment.

pip install Flask

The above command can be run directly, without virtual environment for system-wide installation.

Flask —- Application

In order to test FLASK installation, type the following code in the editor as Hello.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World’

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

Importing flask modules inn the project is mandatory. An object of flask class is our WSGI application.
Flask constructor takes the name of current module(__name__) as argument.

The route() function of the flask class is a decorator,which tells the application which URL should call the associated function.

app.route(rule, options)

The rule parameter represent URL binding with the function.
The options is list of parameters to be forwarded to the underlying Rule object.

In the above example, ‘/’ URL is bound with hello_world() function. Hence, when the home page of web server is opened in browser, the output of this function will be rendered.

Finally the run() method of Flask class runs the application on the local development server.

app.run(host, port, debug, options)

All the parameters are Optional.

The above given Python script is executed from Python shell.

Python Hello.py

A message in Python shell informs you that

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Open the above URL (localhost:5000) in the browser. ‘Hello World’ message will be displayed on it.

--

--