What is Flask
Flaskis a web framework. This means flask provides you with tools, libraries and technologies that allow
you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as
a web-based calendar application or a commercial website.
Flask is part of the categories of the micro-framework. Micro-framework are normally framework with
little to no dependencies to external libraries.
Flask is a web application framework written in Python. It was developed by Armin Ronacher, who led a
team of international Python enthusiasts called Poocco. Flask is based on the Werkzeg WSGI toolkit and
the Jinja2 template engine.Both are Pocco projects.
3.
WSGI
The Web ServerGateway Interface (Web Server Gateway Interface, WSGI) has been used as a standard for
Python web application development. WSGI is the specification of a common interface between web
servers and web applications.
Werkzeug
Werkzeug is a WSGI toolkit that implements requests, response objects, and utility functions. This enables
a web frame to be built on it. The Flask framework uses Werkzeg as one of its bases.
jinja2
jinja2 is a popular template engine for Python.A web template system combines a template with a specific
data source to render a dynamic web page.
First Flask application
Openany text editor (notepad, notepad++ .etc) of your choice Write the following lines of code and save
to a file named as filename.py Here we save it as a.py
from flask import Flask
app = Flask(__name__) #creating the Flask class object
@app.route('/') #decorator defines the
def home():
return "hello, this is our first flask website";
if __name__ =='__main__':
app.run(debug = True)
6.
run this pythoncode on the command line and check the result.
Output:
7.
To build thepython web application, we need to import the Flask module. An object of the Flask class is
considered as the WSGI application.
We need to pass the name of the current module, i.e. __name__ as the argument into the Flask
constructor.
The route() function of the Flask class defines the URL mapping of the associated function. The syntax is
given below.
Explanation
app.route(rule, options)
It accepts the following parameters.
rule: It represents the URL binding with the function.
options: It represents the list of parameters to be associated with the rule object
As we can see here, the / URL is bound to the main function which is responsible for returning the server
response. It can return a string to be printed on the browser's window or we can use the HTML template
to return the HTML file as a response from the server.
Finally, the run method of the Flask class is used to run the flask application on the local development
server.The syntax is given below.
app.run(host, port, debug, options)