Routing in Flask

One of the biggest advantages of Flask is its ability to create routes. A route is a web entry in which we can render a page or serve an endpoint of a RESTful service.

To create routes with Flask, we must use the @route annotation, which will receive the route that we will respond with as a parameter. It is necessary to associate a function that carries out the processing of the request with this annotation.

We could define a route in Flask in the following way:

@app.route("/message/")
def message(name):
return "Welcome "+name+"!"

Now, we are going to create a route that receives a name as a parameter and returns a reply message. This will help us to see how we can pass parameters in Python Flask routes. The first thing is to focus on the route, that is, @app.route('/message/<name>',methods=['GET']).

Now, let's define a method that addresses this route. The peculiarity of this method will be that it must have a parameter, which will correspond to the variable of the route. Now, we can use this variable within the method. In our case, we have used it in the response as part of the greeting. Finally, the entire route will remain as follows:

@app.route('/message/<name>',methods=['GET'])
def message(name):
return Welcome' + name+ '!'

If we return our previous flaskapp_demo.py script, we will have defined a set of functions, usually called views, that handle requests for various parts of our URL. index() and show_docstring() are such functions. You will see that both are preceded by a Flask decorator function, app.route(). This tells Flask which parts of our URL space the decorated function should handle. That is, when a request comes in with a URL that matches a pattern in an app.route() decorator, the function with the matching decorator is called to handle the request. View functions must return a response that Flask can return to the client, but we'll cover more on that in a moment.

The URL pattern for our index() function is just the site root, /, meaning that only requests for the root will be handled by index().

In index(), we just compile our output HTML as a string – first, our list of links to the functions' pages, then a header – and then we return the string. Flask takes the string and creates a response out of it, using the string as the response body and adding a few HTTP headers. In particular, for str return values, it sets Content-Type to text/html.

The show_docstrings() view does a similar thing – it returns the name of the built-in function we're viewing in an HTML header tag, plus the docstring wrapped in a <pre> tag (to preserve new lines and whitespace).

The interesting part is the app.route('/functions/<func_name>') call. Here, we're declaring that our functions' pages will live in the functions directory, and we're capturing the name of the requested function using the <func_name> segment.

Flask captures the section of the URL in angle brackets and makes it available to our view. We pull it into the view namespace by declaring the func_name argument for show_docstring().

In the view, we check that the name that's supplied is valid by seeing whether it appears in the docstrings dictionary. If it's okay, we build and return the corresponding HTML. If it's not okay, then we return a 404 Not Found response to the client by calling Flask's abort() function. This function raises a Flask HTTPException, which, if not handled by our application, will cause Flask to generate an error page and return it to the client with the corresponding status code (in this case, 404). This is a good way to fail fast when we encounter bad requests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset