Flask: Static Templates (Pt.6)

Flask: Static Templates (Pt.6)

Hi there!

In this article, we'll be talking about how to render static templates, HTML templates and file structure strategies.

Static Templates

Static Templates are HTML files that remain constant.

Rendering HTML Templates

HTML can be rendered in Flask using two methods:

  • a string
  • render_template function

A String

You can use HTML as a string in the view function.

I've already set up a virtual environment on for this if you'd like to follow that up you can do that here.

Code!

from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "<h1>There's Something About Chocolate</h1>"


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=3000)

image.png

image.png

render_template() function

For practical applications, it is best to keep the code and templates separate otherwise returning an HTML template as a string would've been enough.

The HTML code is kept in separate files and referred to in the views by their names. And the function, render_template() is what Flask uses to render HTML templates.

This method has the following parameters:

  • template_name_or_list: the name of the template or a list of templates
  • context: variables that should be available inside the template.

The output of the render_template() is then returned by the view instead of a simple string.

def view_name():
    return render_template(template_name)

File Structure Strategies

Flask looks for HTML template files in the \templates directory. This directory can be placed using the following file structures so that Flask can find it.

Module File Structure

In the modular file structure, the application logic is in one .py file, there's a separate directory called templates which will contain the HTML files.

image.png

Package File Structure

If the application logic is divided into separate modules (separate .py files), then these files are present in the same package. In Python, a package is simply a directory containing an __init__.py file.

If this structure is what we're going to use in our application, then we will create the templates directory inside the main application package.

image.png

Using the package file structure approach...

We will render the home.html template using the render_template() function.

In this application, I created a templates directory containing an HTML file called, home.html.

image.png

<!DOCTYPE html>
<html>
<h1>It's Iron Man for me :)</h1>
</html>

In the application module (app.py), I imported the render_template function and home (the view function). This view function (home()) renders the home.html file.

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def home():
    return render_template("home.html")


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=3000)

image.png

Result?

image.png

References:

- - - - - - -

See you in my next post :)