Flask routing allows you to define URL patterns for your web application and associate them with corresponding functions or views.
In this article, we’ll explore Flask routing in detail with some examples.
Creating a Flask Route
Flask is known for its user-friendly routing system that allows you to associate specific URLs with functions or views in your application.
This routing process is managed by the Flask app object that is initialized using the Flask class.
To establish a route in Flask, you can use the route() decorator to link a URL with a function.
For instance, if you want to create a route for the URL /welcome, you can use the following code as an example:
@app.route('/welcome') def welcome(): return 'Welcome to MRX Flask Tutorial'
In the above example, the route() decorator is utilized to bind the URL /welcome with the welcome() function. Whenever a user requests the URL /welcome from your application, Flask will invoke the welcome() function and deliver the string Welcome to MRX Flask Tutorial as the output.
Similar to the route() method showcased in the previous example, the Flask application object also provides the add_url_rule() function that can be applied to link a URL with a function.
The following code snippet serves the same purpose as a decorator:
def welcome(): return 'Welcome to MRX Flask Tutorial' app.add_url_rule('/', 'welcome', welcome)
Dynamic Flask Routing
In Flask, you can use variable components in routes to capture parts of the URL and pass them as arguments to your view functions.
This allows you to create dynamic routes that can handle various inputs.
For instance, if you want to create a route that accepts a user ID as a variable component, you can do so by using the following code:
@app.route('/greetings/<int:user_id>') def display_user(user_id): return 'Hello %d' % user_id
In the above example, the part of the route definition that reads int:user_id specifies that the user ID should be an integer value. Whenever a client requests a URL like /greetings/17, the show_user() function will be called by Flask, passing the value 17 as the user_id argument.
To map URLs with multiple variable components and static components that don’t change, Flask routes can be used.
In such cases, a combination of variable and static components is used in the route definition.
For instance, if you want to create a route that contains a variable username and a static page name, you can use the following code:
@app.route('/greetings/<username>') def show_profile(username): return 'Heyy %s how are you?' % username
In Flask, you can also use the redirect() function to redirect clients to a different URL.
For instance, if you want to redirect clients from the URL / to /hello, you can use the following code:
@app.route('/') def func(): return redirect('/greetings')
The routing system in Flask is a useful feature that enables you to map URLs to specific views or functions in your application. This is achieved through the use of the route() decorator, which can include variable components to create flexible and dynamic routes to handle different client requests. Additionally, Flask’s redirect() function lets you redirect clients to different URLs when required. Together, these features make Flask a powerful and flexible web framework for Python developers.