Python - Routing

Vishal Mane

9 months ago

Routing is the mechanism of mapping the URL directly to the code that creates the webpage. It helps in better management of the structure of the webpage and increases the performance of the site considerably and further enhancements or modifications become really straight forward. In python, routing is implemented in most of the web frameworks. We will see examples from the flask web framework in this chapter.

Routing in Flask

The route() decorator in Flask is used to bind an URL to a function. As a result, when the URL is mentioned in the browser, the function is executed to give the result. Here, URL '/hello' rule is bound to the hello_world() function. As a result, if a user visits http://localhost:5000/ URL, the output of the hello_world() function will be rendered in the browser.

from flask import Flask
app = Flask(__name__)

def hello_world():
   return 'Hello InsideAIML'

if __name__ == '__main__':
When we run the above program, we get the following output

 * Serving Flask app "flask_route" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on (Press CTRL+C to quit) - - [06/Aug/2018 08:48:45] "GET / HTTP/1.1" 200 - - - [06/Aug/2018 08:48:46] "GET /favicon.ico HTTP/1.1" 404 - - - [06/Aug/2018 08:48:46] "GET /favicon.ico HTTP/1.1" 404 -
We open the browser and point to the URL http://localhost:5000/ to see the result of the function being executed.
Hello InsideAIML

Using URL Variables

We can pass on URL variables using route to build URLS on the fly. For this we use the url_for() function which accepts name of the function as the first argument and the rest of the arguments as variable part of the URL rule.
In the below example we pass the function names as arguments to the url_for function and print out the result when those lines are executed.

from flask import Flask, url_for
app = Flask(__name__)

def index(): pass

def login(): pass

def profile(username): pass

with app.test_request_context():
    print url_for('index')
    print url_for('index', _external=True)
    print url_for('login')
    print url_for('login', next='/')
    print url_for('profile', username='InsideAIML')
When we run the above program, we get the following output



We can use the redirect function to redirect the user to another URL using routing. We mention the new URL as a return value of the function which should redirect the user. This is helpful when we temporarily divert the users to a different page when we are modifying an existing webpage.

from flask import Flask, abort, redirect, url_for
app = Flask(__name__)

def index():
    return redirect(url_for('login'))

def login():
#    this_is_never_executed()
When the above code is executed, the base URL goes to the login page which uses the abort function so that the code for the login page is never executed.
I hope you enjoyed reading this article and finally, you came to know about Python - Routing
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review