WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Flask – Application


So as to test Flask installation, type the following code in the manager as Hello.py
from flask import Flask
app = Flask(__name__)

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

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

Importing cup module in the task is obligatory. An object of Flask class is our WSGI application.

Flask constructor takes the name of current module (__name__) as argument.

The route() capacity 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 binding to URL official with the function.

  • The options is a list of parameters to be sent to the underlying Rule object.

In the above precedent, '/' URL is bound with hello_world() work. Thus, when the landing page of web server is opened in program, the yield of this capacity 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
1

host

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

2

port

Defaults to 5000

3

debug

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

4

options

To be forwarded to underlying Werkzeug server.

Debug mode

A Flask application is begun by calling the run() technique. In any case, while the application is a work in progress, it ought to be restarted physically for each adjustment in the code. To keep away from this bother, empower debug support. The server will at that point reload itself if the code changes. It will likewise give a valuable debugger to follow the mistakes assuming 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 investigate parameter to the run() method.

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