Introduction
Flask is one of the most popular microframeworks of Python for creating Web Applications.
Flask’s framework is more explicit than other Python frameworks and easier to learn because it has significantly less base code to implement a simple Web-Application.
App routing is the technique used to map the specific URL with the associated function intended to perform some task. The Latest Web frameworks use the routing technique to help users remember application URLs.
Let’s understand the Routing concepts in depth.
Routing In Flask
App routing is the technique used to map the specific URL with the associated function intended to perform some task. The Latest Web frameworks use the routing technique to help users remember application URLs. It is helpful to access the desired page directly without navigating from the home page.
The URL (‘/’) is linked with the root URL in every application.
Let’s say if our website domain is www.codingninjas.com and we want to add routing to “www.codingninjas.com/hello”, we will use “/hello”.
There are two ways through which we can bind the URLs to their associated function:-
1.) Using route() decorator
2.) Using add_url_rule() function
Using route() decorator:-
In the following example, we bind the URLs (“/”) and (“/hello”) with the root() and hello() functions, respectively, using the app.route() decorator.
In app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def root():
return "Coding Ninjas!!"
@app.route("/hello")
def hello():
return "Hello Coders, Welcome to Coding Ninjas!!"
if __name__ == "__main__":
app.run(debug=True)
The root() function is mapped with the “/” (root) path, and the hello() function is mapped with the “/hello” path, and we will get the output of the functions rendered on the browser for those paths.
Run the above file as a Python file. You will get the following output.
Output:
We will see our application is by default running on port 5000.
URL: localhost:5000.
URL: localhost:5000/hello
Using add_url_rule() function:-
Another way of URL mapping is by using the add_url_rule() function. This approach is mainly used to import the view function from another module. In fact, the app.route() calls this function internally.
The syntax to use this function is:-
add_url_rule(<url rule>, <endpoint>, <view function>)
In the following example, we bind the URLs (“/”) and (“/hello”) with the root() and hello() functions respectively using add_url_rule() function.
In app.py
from flask import Flask
app = Flask(__name__)
def root():
return "Coding Ninjas!!"
def hello():
return "Hello Coders, Welcome to Coding Ninjas!!"
app.add_url_rule('/','root',root);
app.add_url_rule('/hello','hello',hello);
if __name__ == "__main__":
app.run(debug=True)
Run the above file as a Python file. You will get the following output.
Output:
We will see our application is by default running on port 5000.
URL: localhost:5000.
URL: localhost:5000/hello
In the following section, we will learn about Dynamic routing in Flask.