FLASK ROUTING


Let me introduce you with the routing concepts of the Flask framework. Routing is a major and the 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

@app.route(‘/hello’)
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.

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 keeping the code little cleaner.

Are you getting interest?? There's a lot to go.. stay tuned...











Comments

Popular Posts