Flask
Flask
the route() decorator in Flask is used to bind a URL to a function. Now to extend
this functionality our small web app is also equipped with another method
add_url_rule() which is a function of an application object that is also available
to bind a URL with a function as in the above example, route() is used.
Example:
def gfg():
return ‘geeksforgeeks’
app.add_url_rule(‘/’, ‘g2g’, gfg)
Output:
geeksforgeeks
You can also add variables in your web app, well you might be thinking about how
it’ll help you, it’ll help you to build a URL dynamically. So let’s figure it out
with an example.
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
if __name__ == '__main__':
app.run()
And go to the URL http://127.0.0.1:5000/hello/geeksforgeeks it’ll give you the
following output:-
All Flask applications must create an application instance. The web server passes
all
requests it receives from clients to this object for handling, using a protocol
called Web
Server Gateway Interface (WSGI). The application instance is an object of class
Flask,
usually created as follows:
from flask import Flask
app = Flask(__name__)
The only required argument to the Flask class constructor is the name of the main
module or package of the application. For most applications, Python’s __name__
variable
is the correct value.Flask uses
this argument to determine the root path of the application so that it
later can find resource files relative to the location of the application.
The following example defines a route that has a dynamic name component:
@app.route('/user/<name>')
def user(name):
return '<h1>Hello, %s!</h1>' % name
The portion enclosed in angle brackets is the dynamic part, so any URLs that match
the
static portions will be mapped to this route. When the function is invoked, Flask
sends the dynamic component as an argument. In the earlier example function,
this argument is used to generate a personalized greeting as a response.
The dynamic components in routes are strings by default but can also be defined
with
a type. For example, route /user/<int:id> would match only URLs that have an
integer
in the id dynamic segment. Flask supports types int, float, and path for routes.
The
path type also represents a string but does not consider slashes as separators and
instead
considers them part of the dynamic component.
The application instance has a run method that launches Flask’s integrated
development
web server:
if __name__ == '__main__':
app.run(debug=True)
The __name__ == '__main__' Python idiom is used here to ensure that the develop‐
ment web server is started only when the script is executed directly. When the
script is
imported by another script, it is assumed that the parent script will launch a
different
server, so the app.run() call is skipped.
Once the server starts up, it goes into a loop that waits for requests and services
them.
This loop continues until the application is stopped, for example by hitting Ctrl-
C.
There are several option arguments that can be given to app.run() to configure the
mode of operation of the web server. During development, it is convenient to enable
debug mode, which among other things activates the debugger and the reloader. This
is
done by passing the argument debug set to True.