All Courses

Python - Routing

Vishal Mane

a year ago

Python - Routing | insideAIML
Table of Contents
  • Routing in Flask
  • Using URL Variables
  • Redirects
          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
Liked what you read? Then don’t break the spree. Visit our insideAIML blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Page to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing. 

Submit Review