Table of contents
1.
Introduction
2.
Routing In Flask
3.
Dynamic Routing In Flask
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

App Routing In Flask

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)
You can also try this code with Online Python Compiler
Run Code


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>)
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code


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.

Dynamic Routing In Flask

It is possible to make a URL dynamically by adding the variable parts to the rule parameter. This variable part is identifiable as <variable-name>. It is passed as an argument to the function associated with the rule.

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

In app.py

from flask import Flask
app = Flask(__name__)

@app.route('/hello/<name>')
def hello_name(name):
  return 'Hello '+ name

if __name__ == '__main__':
  app.run(debug = True)
You can also try this code with Online Python Compiler
Run Code

 

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/hello/Ninjas

Refer to know about, PHP For Loop

FAQs

  1. What is App 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.
     
  2. List the different methods to bind the URLs to their associated functions.
    There are two ways through which we can bind the URLs:-
    a.) Using route() decorator
    b.) Using add_url_rule() function
     
  3. What is the root URL?
    ‘/’ URL is the root URL for any application.

Key Takeaways

In this blog, we have learned about app routing in Flask, the uses of routing in an application, dynamic routing in Flask, and a few code examples.

If you want to read more blogs related to Flask, you can visit Flask Introduction And Overview and Flask Environment and First Application. If you want to learn advanced web development blogs, you can visit Coding Ninjas Web Blogs.

Happy Learning!!

Live masterclass