Menu Close

Flask App Routing

In this article, we are going to learn all about flask app routing. Flask app routing is the most important part of any flask-based application. In the previous flask tutorial, we have seen how to build the first flask application.

In this guide, we will demonstrate to you, how to set up the route of the flask application to access the different pages according to the route.

In our previous tutorial, we have used only a single route to return a string on the web browser but here we will use a different route.

Before going further, we will see a little bit about routing in web applications why we need to route.

What is routing?

In Web applications. routing is the mechanism by which requests are routed to the code. Routing or router determines what page should be loaded or open when a visitor opens a specific URL with a specific HTTP method.

Why we need of routing?

Routing or Router plays the most important role in any website because routing handles which page should be loaded when specific URL access by the visitors with the help of HTTP methods (GET, POST, PUT, DELETE, PATCH, etc).

Let’s understand some different routing in a flask with the help of examples.

Example

In this example, we will use slash ( / ) for simple flask routing. slash represents the base URL of the website.


from flask import Flask

#Create the object of the Flask class.
app= Flask(__name__)

#Define route 
@app.route("/")
def Home():
    return "This is my first flask website"

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

When the above program will execute, you will get an output “This is my first flask website” in the browser by visiting http://127.0.0.1:5000 address.

Example:

In this example, we will use different routing. basically in this flask app routing. When you will type any string in the browser, The string will be shown on the browser.


from flask import Flask

#Create the object of the Flask class.
app= Flask(__name__)

#Define route 
@app.route("/<name>")
def Home(name):
    return f"My name is {name}"

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

The above code will produce the following result in the browser.

add_url_rule() function

Flask provides another best function add_url_rule() to define the routing in the flask application rather than the route() method.

The syntax of the add_url_rule() function is given below.

add_url_rule(<url rule>, <endpoint>, <view function>)

Example:

In this example, we have created a simple flask application routing using add_url_rule()function.


from flask import Flask

#Create the object of the Flask class.
app= Flask(__name__)

#Define function 
def Home():
    return "This is my about page of the website."
    
#define the routing   
app.add_url_rule("/about", "about", Home)

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

Run the above code by executing the following command.

python python_filename

After successfully executed the above code, you have to visit the http://127.0.0.1:5000/about address to see the result in the browser.

Conclusion

In this example, we have learned all about flask app routing using different-2 examples. Router or routing is the most important thing in any web application. To learn more about the flask web framework, follow our python flask tutorial.

If you like this tutorial, please share and keep visiting for further flask tutorials.

Python Flask Tutorials


Previous Article
Next Article

Related Posts