M/s VIKASH TECH - We Write Imaginations to Codes..!!


Let me introduce you to the routing concepts of the Flask framework. Routing is a major and basic part of this framework as it defies which data has to be forwarded to the client on different URL requests.
Moving to the depth of routing, route() is called the decorator in Flask, which is used to bind URL to functions as shown below.

Python Code

def hello_world():
   return ‘hello world’

In the above line of codes, the URL ‘/hello‘ rule is bounded to the hello_world() function, due to which if a client requests for the hello page using URL – http://localhost:5000/hello  an output of “hello world” is displayed on the browser.
The add_url_rule() function of an application object is also available to bind a URL with a function as in the above example, route() is used.
Also, use of add_url_route() function can be done to bind a URL with a function as done by route() function in above example. How?? Let’s see.

Python Code

def hello_world():
   return ‘hello world’
app.add_url_rule(‘/’, ‘hello’, hello_world)

Here also, the same thing is being performed, the difference is, you do not need to define the function just below the decorator, you can define the functions first and then add the URL rules at the end, one after another. It helps to keep the code a little cleaner.
Are you getting interested?? There’s a lot to go.. stay tuned…

Category :


No comments found.



Latest Posts

Templates - In Flask

Templates are the structure of a website. Here in Python Flask. templates are treated as the views, which means these…

Static Files - Flask

When you work for web development, you will need to use some files for javascript or CSS, here in FLASK…

Variables in Flask

In Flask, a variable is created using tags inside route() function. To create a variable you just need to put…

Flask - Binding URL

First thing that comes to mind when we hear binding is - I guess - BOOKBINDING / PAPER BINDING. Well,…