Techno Blender
Digitally Yours.

Passing URL Arguments in Flask

0 44


Improve Article

Save Article

Like Article

Improve Article

Save Article

In this article, we will cover how to Pass URL Arguments in Flask using Python. URL converters in Flask are mentioned in angular brackets (<>). These unique converters are designed to let us generate extremely dynamic URLs, where a part of the URL is regarded as a variable. For that we have created three different endpoints to understand three different scenarios of URL converters in Flask.

  1. Usage of a single URL converter.
  2. Usage when there are multiple URL converters.
  3. Use of type hints for addressing specific datatypes for values passed in the URL.

Consider the URL for each person’s social media profile page as an example of where we can have the URLs as –

  • https://www.faceconnect.com/home/user/1001
  • https://www.faceconnect.com/home/user/1002
  • https://www.faceconnect.com/home/user/1003

Here, the base URL is https://www.faceconnect.com/home/user/ and 1001, 1002, and 1003 are the user IDs which extend the URL to point to the specific user’s homepage.

Single URL Converter

In this example, a single endpoint with the prefix “/home” has been built.

  • The word “menu” has been put between angular brackets, indicating that it is a dynamic element of the URL. It will accept any text value.
  • For instance, we attempted “/home/dashboard” (as seen in the output), which produced a dashboard as the value of the menu argument.
  • However, attempting to visit “/home/,” “/home/dashboard/,” or “/home/dashboard/views” will result in an error, indicating that the path cannot contain additional routes and that the value must be present.

Python3

from flask import Flask, request

  

app = Flask(__name__)

  

  

  

@app.get('/home/<menu>')

def single_converter(menu):

  

    return "You tried excessing 'single_converter' \

    endpoint with value of 'menu' as " + str(menu)

  

  

if __name__ == '__main__':

    

    app.run(debug=True)

Output:

Output – Example 1

Multiple URL Converter

In this example, we’ve built an endpoint that has both a menu and a submenu, two dynamic parameters.

  • Be aware that even though we utilise these arguments to create our route, we also provide them as parameters in the function declaration.
  • This route will work for the URL where the home route is followed by two further routes.
  • However, as indicated in the output, attempting to access “/home/dashboard/views/” will result in a failure. 

Python3

from flask import Flask, request

  

app = Flask(__name__)

  

@app.get('/home/<menu>/<submenu>')

def multiple_converter(menu, submenu):

  

    return "You tried excessing 'multiple_converter' \

    endpoint with value of 'menu' and 'submenu' as " \

        + str(menu) \

        + ' and ' \

        + str(submenu) \

        + ' respectively'

  

if __name__ == '__main__':

    

    app.run(debug=True)

Output:

Passing URL Arguments in Flask

Output – Example 2

URL Converter with DataType 

The last example uses an endpoint that we developed to type-cast the dynamic URL to the specified data type.

  • Here, we have said that an article ID, which must be of the integer data type, follows the article path.
  • Therefore, the endpoint will successfully process any integer value that is passed to it in the route.
  • It will give an error if it is unable to typecast the value to an integer data type, such as alphanumeric characters. (As shown by the output)
  • It will typecast to the string data type by default if no type is specified.

Python3

from flask import Flask, request

  

app = Flask(__name__)

  

  

  

@app.get('/article/<int:article_id>')

def converter_with_type(article_id):

  

    return "You tried excessing 'converter_with_type' \

    endpoint with value of 'article_id' as " \

        + str(article_id) \

        + ' and data type as' \

        + str(type(article_id))

  

  

if __name__ == '__main__':

    

    app.run(debug=True)

Output:

Passing URL Arguments in Flask

Output – Example 3


Improve Article

Save Article

Like Article

Improve Article

Save Article

In this article, we will cover how to Pass URL Arguments in Flask using Python. URL converters in Flask are mentioned in angular brackets (<>). These unique converters are designed to let us generate extremely dynamic URLs, where a part of the URL is regarded as a variable. For that we have created three different endpoints to understand three different scenarios of URL converters in Flask.

  1. Usage of a single URL converter.
  2. Usage when there are multiple URL converters.
  3. Use of type hints for addressing specific datatypes for values passed in the URL.

Consider the URL for each person’s social media profile page as an example of where we can have the URLs as –

  • https://www.faceconnect.com/home/user/1001
  • https://www.faceconnect.com/home/user/1002
  • https://www.faceconnect.com/home/user/1003

Here, the base URL is https://www.faceconnect.com/home/user/ and 1001, 1002, and 1003 are the user IDs which extend the URL to point to the specific user’s homepage.

Single URL Converter

In this example, a single endpoint with the prefix “/home” has been built.

  • The word “menu” has been put between angular brackets, indicating that it is a dynamic element of the URL. It will accept any text value.
  • For instance, we attempted “/home/dashboard” (as seen in the output), which produced a dashboard as the value of the menu argument.
  • However, attempting to visit “/home/,” “/home/dashboard/,” or “/home/dashboard/views” will result in an error, indicating that the path cannot contain additional routes and that the value must be present.

Python3

from flask import Flask, request

  

app = Flask(__name__)

  

  

  

@app.get('/home/<menu>')

def single_converter(menu):

  

    return "You tried excessing 'single_converter' \

    endpoint with value of 'menu' as " + str(menu)

  

  

if __name__ == '__main__':

    

    app.run(debug=True)

Output:

Passing URL Arguments in Flask

Output – Example 1

Multiple URL Converter

In this example, we’ve built an endpoint that has both a menu and a submenu, two dynamic parameters.

  • Be aware that even though we utilise these arguments to create our route, we also provide them as parameters in the function declaration.
  • This route will work for the URL where the home route is followed by two further routes.
  • However, as indicated in the output, attempting to access “/home/dashboard/views/” will result in a failure. 

Python3

from flask import Flask, request

  

app = Flask(__name__)

  

@app.get('/home/<menu>/<submenu>')

def multiple_converter(menu, submenu):

  

    return "You tried excessing 'multiple_converter' \

    endpoint with value of 'menu' and 'submenu' as " \

        + str(menu) \

        + ' and ' \

        + str(submenu) \

        + ' respectively'

  

if __name__ == '__main__':

    

    app.run(debug=True)

Output:

Passing URL Arguments in Flask

Output – Example 2

URL Converter with DataType 

The last example uses an endpoint that we developed to type-cast the dynamic URL to the specified data type.

  • Here, we have said that an article ID, which must be of the integer data type, follows the article path.
  • Therefore, the endpoint will successfully process any integer value that is passed to it in the route.
  • It will give an error if it is unable to typecast the value to an integer data type, such as alphanumeric characters. (As shown by the output)
  • It will typecast to the string data type by default if no type is specified.

Python3

from flask import Flask, request

  

app = Flask(__name__)

  

  

  

@app.get('/article/<int:article_id>')

def converter_with_type(article_id):

  

    return "You tried excessing 'converter_with_type' \

    endpoint with value of 'article_id' as " \

        + str(article_id) \

        + ' and data type as' \

        + str(type(article_id))

  

  

if __name__ == '__main__':

    

    app.run(debug=True)

Output:

Passing URL Arguments in Flask

Output – Example 3

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment