3
Routing in Flask
Application
Mr. Shekhar D Jalane
Web Technology-I
Routing Technique
2
Modern web frameworks use the routing
technique to help a user remember
application URLs.
The route() decorator in Flask is used to
bind URL to a function.
@[Link]('/hello’) # @[Link]('/')
def hello_world():
return 'Hello World’
Web Technology-I
Flask Application
3
from flask import Flask
app = Flask(__name__)
@[Link]('/’)
def hello_world():
return 'Hello World’
if __name__ == '__main__’:
[Link]()
Web Technology-I
Bind Rule
4
URL ‘/hello’ rule is bound
the hello_world() function.
As a result, if a user
visits [Link]
he output of the hello_world() function
will be rendered in the browser
Web Technology-I
add_url_rule()
5
def hello_world():
return ‘hello world’
app.add_url_rule(‘/’, ‘hello’, hello_world)
Web Technology-I
Add multiple Routes
6
from flask import Flask
app = Flask(__name__)
@[Link]('/flask') #Add /flask/ URL results in 404 Not
Found page
def hello_flask():
return 'Hello Flask'
@[Link]('/python/')
def hello_python():
return 'Hello Python'
if __name__ == '__main__':
[Link]()
Web Technology-I