Use a template in Flask

One of the simplest things that can be done in Flask is to create a web page upon request. That is, use a template in Flask that is a page HTML to which we can insert content retrieved from our program Python.

The first thing will be to create a route to accept a request. In this case we use the root of the server.

@app.route('/')
def greeting():

Once we receive a request on this route we will use the method as a return
render_template which will indicate as a parameter the name of the template to be loaded.

 return render_template('hello.html')

The templates in Flask are in the templates directory

In the event that we want to pass parameters, we will put these below.

@app.route('/')
def greeting():
  name = 'Victor'
  return render_template('hello.html',name=name)

Now we move on to creating the template. You have to create a file HTML in the templates directory. Inside the file HTML we can dump the content of the variable using its name in braces.

<!DOCTYPE html>
<html lang="es">
<head>
<title>Hello World</title>
</head>
<body>

Hello {{ name }}

</body>
</html>

In this simple way we will have managed to use a template in Flask.