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__)

def hello_world():
   return 'Hello World’

if __name__ == '__main__':

Importing flask module in 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)

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 parameters are optional

Sr.No. Parameters & Description


Hostname to listen on. Defaults to (localhost). Set to ‘’ to have server available externally



Defaults to 5000



Defaults to false. If set to true, provides a debug information



To be forwarded to underlying Werkzeug server.

The above given Python script is executed from Python shell.

Python Hello.py

A message in Python shell informs you that

* Running on (Press CTRL+C to quit)

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

Debug mode

A Flask application is started by calling the run() method. However, while the application is under development, it should be restarted manually for each change in the code. To avoid this inconvenience, enable debug support. The server will then reload itself if the code changes. It will also provide a useful debugger to track the errors if any, in the application.

The Debug mode is enabled by setting the debug property of the application object to True before running or passing the debug parameter to the run() method.

app.debug = True
app.run(debug = True)