← Teacher guide

A page from Python

Students meet Flask, where each address is a route joined to a function that returns the page as HTML, then add a results page, a visit counter and a small site of their own.

Web apps with Flask · Pages from Python · about 50 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 10 min · Modify 20 min · Make 10 min

Before class

Read the unit primer first. This course runs in the Python playground (real Python in the browser, Pyodide), where app.run() shows the site in an App tab with an address bar; links and forms in the page go back to the app. On a computer, pip install flask and python app.py serve the same file at http://127.0.0.1:5000. Before teaching it, open the site's Check your school page once on a student device at school. A route (@app.route("/about"), a decorator: the line with @ above a function) joins an address to a function; the function returns the page as HTML text. An address with no route gives the standard 404 Not Found. HTML inside Python strings uses single quotes for attributes (href='/about') so they do not end the string. The visit counter uses global (from the turtle course) and resets on every Run: variables last only while the program runs, which sets up saving in Lesson 4.

Running the lesson

  1. Predict: draw a table on the board: address on the left, function on the right.
  2. Run: have students type /missing and read the 404 page.
  3. Investigate: ask what the browser receives: just text, which happens to be HTML.
  4. Modify: visit the counter page five times, then press Run, and ask why it reset.
  5. Make: sketch the site's pages and links on paper first.

The hard part (on purpose)

Understanding that the browser asks and the function answers. Ask students to say which function runs for each address before they click.

When students get stuck

What you will seeA question to askThe fix
Not Found for a page they wrote.Is there an @app.route line directly above the function, with the right address?Add @app.route("/results") above def results():.
SyntaxError on a line of HTML.Do the quotes inside the HTML match the quotes around the string?Use single quotes inside: "<a href='/about'>About</a>".
UnboundLocalError: local variable 'visits' referenced before assignment.Does home() say it is changing the variable outside it?global visits as the first line of home().

Answers to the checks

  1. Predict: Someone visits /about. What does the site send back?
    The About page. @app.route("/about") joins the address /about to the function about(), so Flask calls it and sends back the HTML it returns.
  2. An address with no route: What does a visitor see at /missing?
    A Not Found page (error 404): no route matches that address
    Flask looks for a route that matches the address. With none, it sends back the standard 404 Not Found page, as every website does.
  3. Building the page: home() builds page with page = page + ... three times. What is the page?
    One piece of HTML text, joined from three parts
    page is text; each line adds more HTML to the end. The browser shows the whole thing as one page.
  4. Spot the bug: Clicking "About the club" shows Not Found, though about() is written. Click the line where the problem is.
    line 11
    about() has no @app.route("/about") above it, so no address leads to it. A function only becomes a page when a route points to it.
  5. Where this leads: The visit counter went back to 0 when you pressed Run. Why?
    visits lives in the running program; a new run starts it again from 0
    Variables last only while the program runs. To keep sign-ups or counts, a web app saves them to a file or a database: that comes in Lesson 4.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › A results page

from flask import Flask

app = Flask(__name__)


@app.route("/")
def home():
    page = "<h1>Chess club</h1>"
    page = page + "<p>Lunchtime on Tuesdays, Room 12.</p>"
    page = page + "<p><a href='/about'>About the club</a></p>"
    page = page + "<p><a href='/results'>Results</a></p>"
    return page


@app.route("/about")
def about():
    return "<h1>About</h1><p>Everyone is welcome, from beginners to champions.</p><p><a href='/'>Home</a></p>"


@app.route("/results")
def results():
    return "<h1>Results</h1><table><tr><th>Player</th><th>Wins</th></tr><tr><td>Ada</td><td>5</td></tr><tr><td>Sam</td><td>4</td></tr></table><p><a href='/'>Home</a></p>"


app.run()

Modify › A visit counter

from flask import Flask

app = Flask(__name__)
visits = 0


@app.route("/")
def home():
    global visits
    visits = visits + 1
    page = "<h1>Chess club</h1>"
    page = page + "<p>Lunchtime on Tuesdays, Room 12.</p>"
    page = page + "<p><a href='/about'>About the club</a></p>"
    page = page + "<p>This page has been visited " + str(visits) + " times.</p>"
    return page


@app.route("/about")
def about():
    return "<h1>About</h1><p>Everyone is welcome, from beginners to champions.</p><p><a href='/'>Home</a></p>"


app.run()

your own site

from flask import Flask

app = Flask(__name__)


@app.route("/")
def home():
    return "<h1>Robotics club</h1><p><a href='/times'>When we meet</a> · <a href='/robots'>Our robots</a></p>"


@app.route("/times")
def times():
    return "<h1>When we meet</h1><ul><li>Monday lunchtime</li><li>Thursday after school</li></ul><p><a href='/'>Home</a></p>"


@app.route("/robots")
def robots():
    return "<h1>Our robots</h1><ul><li>Cutebot</li><li>Line follower</li></ul><p><a href='/'>Home</a></p>"


app.run()

Stretch and support

Stretch: Add a page at /time that shows a different message in the morning and the afternoon (import datetime; datetime.datetime.now().hour).

Support: Give students the results route with its table, and ask them only to add the link from the home page.

Exit ticket

What is a route?

A link between an address in the site, such as /about, and the Python function that makes that page.

What each step is for

Where it leads

Next lesson builds pages from a list of data with a template. Later lessons take form answers, save them to a file and send data as JSON.