Flask with static html files

You can use the Flask framework and use static files together.

Flask will give you URL routing, many features and all the Python benefits.

You may want an application that is partly dynamic and partly static. Or you may simply want to browse with URL routing. In this article we will teach you how to load static HTML files with Flask.

Download Flask Examples

Flask static files example

Create a file called app.py with this contents:

from flask import Flask, render_template
app = Flask(__name__)
def render_static(page_name):
    return render_template('%s.html' % page_name)
if __name__ == '__main__':

This application initializes a Flask app with the method:


The app creates an URL route for any possible page and links that to static html files with:

def render_static(page_name):
    return render_template('%s.html' % page_name)

Create a directory /templates/ and add the file hello.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>Hello World Template</title>
Hello World

Start the server with:

$ python app.py 
* Running on

Then any .html file is accesible using URL routing.

For example the static file hello.html can be accessed using You can store any css file in the /static/ directory.