Python informer

Improve your Python coding skills

Flask webserver - dynamic pages

This article is part of a series on Flask.

The main advantage of Flask is that you are using Python to create your web pages. If you need to do something a bit different to a standard static page, you can just code it in Python.

To illustrate this, we will add another page to our site to print out some status information. We will just add some random facts - the current date, the server operating system and the Python version we are using. You can add some more of your own if you like. You might even do something more involved, such as calling the twitter API to fetch your organisations’s latest tweets, or scraping a website to get a weather forecast, and including that on your page.

A new HTML template

Since we are presenting structured content (a list of information) rather than just a block of text, we will create a new template with some new tags:

    <link rel="stylesheet" href='/static/main.css' />
    Date: {{date}}
    Operating system: {{operating_system}}
    Python version: {{python_version}}

We have removed the {{content}} tag, and replaced it with some extra tags, each in its own paragraph. Save this file as status.html in the templates folder.

Changing the Python code

We need some extra includes at the top of the file. These are needed to get the extra information for the status page:

import platform
import sys
import datetime

And, of course, we will need an extra routing function for our page:

def status():
    dt = str(
    os = platform.system()
    pyver = sys.version
    return render_template('status.html',

The new routing function has a different route, /status, and a different function name status. We are also using the new template, status.html.

The code first obtains the date and time, operating system name and Python version using standard Python calls. Look them up in the Python documentation to find out more. These values are passed as named parameters into render_template().

You can view the status page by visiting with your browser.

The full code for this section can be found on github.