Home » Flask App Routing

Flask App Routing

by Online Tutorials Library

Flask App routing

App routing is used to map the specific URL with the associated function that is intended to perform some task. It is used to access some particular page like Flask Tutorial in the web application.

In our first application, the URL (‘/’) is associated with the home function that returns a particular string displayed on the web page.

In other words, we can say that if we visit the particular URL mapped to some particular function, the output of that function is rendered on the browser’s screen.

Consider the following example.

Example

Flask facilitates us to add the variable part to the URL by using the section. We can reuse the variable by adding that as a parameter into the view function. Consider the following example.

Example

It will produce the following result on the web browser.

Flask App routing

The converter can also be used in the URL to map the specified variable to the particular data type. For example, we can provide the integers or float like age or salary respectively.

Consider the following example.

Example

Flask App routing

The following converters are used to convert the default string type to the associated data type.

  1. string: default
  2. int: used to convert the string to the integer
  3. float: used to convert the string to the float.
  4. path: It can accept the slashes given in the URL.

The add_url_rule() function

There is one more approach to perform routing for the flask web application that can be done by using the add_url() function of the Flask class. The syntax to use this function is given below.

This function is mainly used in the case if the view function is not given and we need to connect a view function to an endpoint externally by using this function.

Consider the following example.

Example

Flask App routing


Next TopicFlask URL Building

You may also like