Flask routing
Modern web frameworks use routing technology to help users remember application URLs. You can directly access the desired page without navigating from the home page.
The route() decorator in Flask is used to bind URLs to functions. E.g:
@app.route(‘/hello’)
def hello_world():
return ‘hello world’
Here, the URL '/hello' rule is bound to the hello_world() function. Therefore, if the user visits http://localhost:5000/hello URL, the output of the hello_world() function will be rendered in the browser.
The add_url_rule() function of the application object can also be used to bind the URL to the function. As shown in the above example, use route() .
The purpose of the decorator is also represented by:
def hello_world():
return ‘hello world’
app.add_url_rule(‘/’, ‘hello’, hello_world)
Flask variable rules
Flask variable rules
By adding a variable part to the rule parameter, the URL can be constructed dynamically. This variable part is marked as <variable-name> . It is passed as a keyword argument to the function associated with the rule.
In the following example, route () rule contains additional parameters decorator to the URL '/ Hello' to <name>. Thus, if the input browser http: // localhost: 5000 / hello / w3cschool as the URL , then 'w3cschool' will be provided as a parameter to the hello () function.
from flask import Flask
app = Flask(__name__)
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
if __name__ == '__main__':
app.run(debug = True)
Save the above script as hello.py and run it from the Python shell. Next, open a browser and enter the URL http://localhost:5000/hello/w3cschool.
The following output will be displayed in the browser:
Hello w3cschool!
In addition to the default string variable part, you can also use the following converters to build rules:
Serial number | Converter and description |
---|---|
1 | int Accept integer |
2 | float For floating point values |
3 | path Accept slashes used as directory separator |
In the following code, all these constructors are used:
from flask import Flask
app = Flask(__name__)
@app.route('/blog/<int:postID>')
def show_blog(postID):
return 'Blog Number %d' % postID
@app.route('/rev/<float:revNo>')
def revision(revNo):
return 'Revision Number %f' % revNo
if __name__ == '__main__':
app.run()
Run the above code from the Python Shell. Visit the URL http://localhost:5000/blog/11 in the browser .
The given number is used as the parameter of the show_blog() function. The browser displays the following output:
Blog Number 11
Enter this URL in the browser- http://localhost:5000/rev/1.1
The revision() function takes a floating point number as a parameter. The following results are displayed in the browser window:
Revision Number 1.100000
Flask's URL rules are based on Werkzeug 's routing module. This ensures that the URL formed is unique and based on the precedent set by Apache.
Consider the rules defined in the following script:
from flask import Flask
app = Flask(__name__)
@app.route('/flask')
def hello_flask():
return 'Hello Flask'
@app.route('/python/')
def hello_python():
return 'Hello Python'
if __name__ == '__main__':
app.run()
These two rules look similar, but in the second rule, a slash (/) is used . Therefore, it becomes a canonical URL. Therefore, using /python or /python/ returns the same output. However, if it is the first rule, the /flask/ URL will generate a "404 Not Found" page.