Flask Web Framework – Routing and Variables Rules

Published by BrighterBees on

flask-routing-and-variables

Routing

Modern web frameworks use the routing technique to help a user remember application URLs. It is useful to access the desired page directly without having to navigate from the home page.

The route() decorator in Flask is used to bind URL to a function. For example –

@app.route('/home')
def home():
    return 'Home page'

Here, URL ‘/home’ rule is bound to the home() functions. As a result, if a user visits http://localhost:5000/home URL, the output of the home() function will be rendered in the browser.

The add_url_route() function of an application object is also available to bind a URL with a function as in the above example, route() is used.

A decorator’s purpose is also served by the following representation –

def home():
    return 'Home Page'
app.add_url_rule('/','home', home)

You can try more routes like home, about, contact and many more.

Variables Rules

It is possible to build a URL dynamically, by adding variable parts to the rule parameter. This variable parts to the rule parameter. The variable part is marked as <variable-name>. It is passed as a keyword argument to the function with which the rule is associated.

In the following example, the rule parameter of route() decorator contains <name> variable part attached to URL ‘/home’. Hence, if the http://localhost:5000/home/brighterbees is entered as a URL in the browser, ‘brighterbees’ will be supplied to home() function as an argument.

from flask import Flask
app = Flask(__name__)

@app.route('/home/')
def home(name):
    return 'Welcome To Home %s!' % name

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

Save the above script as app.py and run it from python shell. or CMD. Next, open the browser and enter URL http://localhost:5000/home/brighterbees.

The Following output will be displayed in the browser.

Welcome to Home brighterbees!

In addition to the default string variable part, rules can be constructed using the following converters –

  • int : accepts integer
  • float : For floating-point value
  • path : accepts slashes used as the directory separator character

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 Python shell or CMD. Visit the URL http://localhost:5000/blog/1 in the browser.

The given number is used as an argument to show_blog() function. The browser displays the following output –

Blog Number 1

Enter the URL in the browser – http://localhost:5000/rev/1.5

The revision() function takes up the floating-point number as argument. The following result appears in the browser window –

Revision Number 1.5000000

The URL rules of Flask are based on Werkzeug’s routing module. This ensures that URLs formed are unique and based on precedents laid down by Apache.

Consider the rules defined in the following code-

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

Both the rules appear similar but in the second rule, trailing slash ( / ) is used. As a result, it becomes a canonical URL. Hence, using /python or /python/ returns the same output. However, in the case of the first rule, /flask/ URL results in 404 Not Found Page.

So till now, we have learned about Flask routing and variables names. I think you have applied it also. If you have any doubt then you can comment below.

Connect with the author on LinkedIn.

If want to know about Web Scraping click here.


1 Comment

ydndozgrwn · February 28, 2021 at 1:07 PM

Flask Web Framework – Routing and Variables Rules – BrighterBees
[url=http://www.gdx16349e190aqdrys40eb4ymi5670r1s.org/]uydndozgrwn[/url]
ydndozgrwn http://www.gdx16349e190aqdrys40eb4ymi5670r1s.org/
aydndozgrwn

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US