The goal of this video is to give an overview of Flask's template support.
- [Narrator] Flask template usage. In the previous video, we looked at how to configure routes and how to handle request permitters in a Flask application. Now let's continue and check how to use templates inside Flask. In this video, we are going to take a look at Flask template engine, what folder structure should an application have for template usage, and how templates can be used and how to create template filters. First, let's take a look at the standard for the structure of a Flask application. Inside the main directory, flask_tutorial, we have two important folders.
Bootswatch is a website which contains free themes. Now let's take a look at our application. Our application starts with the Home screen, where we state that we should select an option from the menu. If we navigate to the About page, then we can see a description about the project and what is its purpose. If we select the Entities menu item, then we can navigate to the Candidates, the Projects, and Experience screen. If we go to the Candidates screen, then we get listed all the senior candidates what we have in our database.
For each candidate, we display the first name, last name, the domain, and the years of experience the candidate has. If we select the Projects screen, then we have two options. The first is to load all the projects. Then we are listed all the projects what we have in our database, and for each project we receive the description, and the ID, name, and start date. The second option is to create a new project. We can do this by specifying the project name and the description.
Now let's see how to use templates in our application. Let's open up the routes.py file, and let's take a look at the page index function. In the page index, we use the render template, Flask_App or method. The first parameter is the name of the template, in our case it's index.html. All the parameters after the template name are passed to the template context, and can be used inside the template. Since these are name parameters, it can be referenced using the same name, in our case selected_menu_item. The routing setup for the website pages is done using the add_url_rule method.
We have covered this method in the previous video. Now let's open up the layout.html file. This is a standard HTML file with some Jinja syntax inside. Jinja engine supports block definitions, which means inside our HTML code, we can define blocks of code which can be redefined in other template files. An example of block definition can be seen in the head section. Block definition starts with the block keyword, and we have to specify a block name, and the blocks end with the endblock keyword. In certain HTML file it can define many blocks.
In our layout.html file, we have five blocks: head, navigation, content, footer, and scripts. This is a base template which contains the HTML skeleton of the website. Its child templates just fill the empty blocks. In the navigation block, we use the URL4 method, and this was presented in the previous video. We use this to generate the rule for the index page. The URL4 method generates a URL to the given endpoint. In the highlighted code region, we can see how paramaters sent through the render template method can be used.
Here we check the if selected menu item's value is index, it means we are on the home page, so the link for the home page should not navigate anywhere. In case the selected menu item does not have the index value, we define the link for the home page using the URL4 method. Please notice that the code inside double curly braces are evaluated, and the result is printed. Usually, the curly brace percentage block contains longer statements, like if statements or for loops.
Now let's open up the candidate.html file. In the candidate.html file, we can see how template inheritance works. Using extends template name structure, we can define what should be the base of our template, and after that we can override any previously defined block. We can see this on the highlighted code. In our case, we override the content block. Below, we are using a for loop to create a new HTML table row for each senior candidate.
Inside the loop, we can access the variable defined in the for loop, candidate, and all its properties. The pipe into senior candidate is a matter for filtering inside Jinja. This is called filtering because the filter methods usually reduce the amount of data, or reduce the data structure size which we are applying this to. The Jinja2 engine has a lot of pretty fine filters, like sort, reverse, upper, lower, URL encode. We can find examples and their usage on this website.
Now let's check the method which renders the template for candidates. We should go back to the code and open up the routes.ty file. First, we load the candidates from the data service, then we use the render template method, specify the template name, then the selected menu item's value and the last parameter is the candidate which we want to display. Please note that the templates are located in the templates folder. The code for the senior candidate filter can be found in the app.py file. This is a normal Python function which receives a parameter.
All this does is cycle through the candidates' experience, and if the candidate has five years or more experience, then we append this candidate's result list. In order to use a method as a template filter, this has to be annotated with a template filter annotation. We can specify the name of the filter as a parameter for annotation. In this video we have covered how to use templates inside Flask. We learned how to define and use template filters. We learned what is a standard folder structure of Flask application. In the next video, we will check how to use Flash messages, and how to create custom error pages.
Thank you for watching, and see you in the next video.
Regardless of platform, you will need to build APIs to serve data between different client applications and endpoints. Good APIs are a necessity for web and mobile projects, especially with the modern, mobile-first approach to development. This course delivers the fundamental knowledge required to enable highly connected interactions between applications via RESTful web APIs. Follow along with Gergo Bogdan and learn how to build up and structure an effective web API that can be used by any client application accessing it over HTTP, using Flask, the Python microframework.
Find out how to implement CRUD operations using SQLAlchemy and MySQL as the data store. Get an understanding of how REST works relative to APIs, and learn how to test APIs written in Python with the support of Flask. Explore token-based authentication and find out how to store passwords securely in your database. Along the way, Gergo introduces best practices and design guidelines when building large applications.
- Setting up Flask
- Using Flask templates and routing
- Implementing CRUD operations
- Implementing pagination
- Configuring RESTful authentication
- HTTP caching
- Securing web APIs
- Testing the application