Files & Templates


So you’ve seen how to send out some basic text with a simple Flask application, and the languages that are used to build the web: HTML, CSS and Javascript. Serving text isn’t enough though - what if we want to serve some actual files?

Luckily, Flask comes prepared. All you have to do is create a folder in the same directory as with the special name of static. Any file you put in that folder will be served by Flask automatically! It’s one of the nice functionalities provided to you by Flask’s authors.

You can do this with your computer’s file browser, or on the command line by typing mkdir static.

So, if you made a file in the static folder called cats.gif, and you’d normally access your application by visiting the url, then you can view that file by going to

Check this works for you by downloading a funny image from the internet (there are plenty at imgur), putting it in the static folder, running your Flask application (python, and browsing to your image by the right url.

The static folder is a great place to put CSS stylesheets, Javascript script files, and images. Those files don’t usually change much when a website is running, hence the folder name. A common practice is that web developers will organise these files in sub-directories called, for instance, css, js, and img respectively.

HTML Templates

None of that explains where to put the content of our application - the HTML. Flask makes use of HTML templating - this allows it to put dynamic application data (like a list of email addresses that changes over time) into HTML easily, so that any web browser can view it easily, and it looks good as well.

Flask uses another special directory to store templates, called templates (crazy, huh?), so go ahead and create it, like you did for static.

In it, create a file called index.html and chuck some in some HTML -

<!DOCTYPE html>
    <title>{{ author }}'s app</title>

    <h2>Hello {{ name }}!</h2>

This is a pretty simple HTML page, but it includes two simple bits of Flask’s templating language. When Flask shows this template to your browser, it will replace {{ author }} and {{ name }} with what you assign those two variables in your application. If author was assigned to be “Bob”, then the title of the page served will be “Bob’s app”.

In this way a template separates what the content of the page should be, from the actual actual data that will be used as that content. This makes things a whole lot easier if you have to write a lot of dynamic web pages!

Flask’s templates can do a good deal more than just display variables, but we can get to that in good time.

Let’s modify our flask app to serve this template:

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

def hello_world():
    author = "Me"
    name = "You"
    return render_template('index.html', author=author, name=name)

if __name__ == "__main__":

You can see we imported a new function from flask, called render_template.

Then in hello_world(), we added two variables called author and name. Please change their values from "Me" and "You" in your file, those are boring!

Then instead of returning some text, we returned the result of calling render_template. First, we give it the file name of the template we wish to process. Then, we pass in the variable names that the template should know about (author and name) and what their values should be (…coincidentally, author and name!). This function should load up the template, do the processing required to replace placeholders with values, and return the full HTML text.

Now try running and viewing your app, in all its HTML glory!

A prettier home page

Now that we have a bit more confidence in changing the home page we can look at making something a bit prettier. We want the home page of Cats Everywhere to be fun and exciting. Something that says “Yes, these people know what they are doing. I will give them my email address in hope of seeing more of their work in future.”

This means fancy fonts, large bold type, huge banner images and nice round corners and things. We are going to need some more interesting HTML and a fair bit of CSS to get it looking pretty.

We’ve prepared a self-contained template that you can use for the home page. You can download it here and save it as index.html in your templates directory.

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>Cats Everywhere!</title>	

    <link     href=',700'     rel='stylesheet' type='text/css'>

    <style type="text/css">    

        font-family:Arial, sans-serif;
        text-shadow:2px 0 15px #292929;

        margin:90px auto auto;
        padding:80px 0;

        box-shadow:rgba(0,0,0,0.2) 0 1px 3px;
        font-family:Sintony, sans-serif;


    <div id="banner">
      <h1>cats everywhere</h1>
      <p class="lead">We're bringing cats to the internet. Free.     Cute. Awesome.</p>
    <div id="emailform">


It should look a little something like this: