By the end of this lesson you will have:
This is a long lesson in five stages. Work through them in order and save evidence at each stage.
BR3 Interactive website
AR1 Dataset website integration
AR2 Additional data
Organise your Artefact folder like this, with your main program clearly named:
Artefact/
app.py <- main program: run this
make_data.py (only if you simulate data)
clean_data.py
charts.py
data/wellbeing.csv
templates/ index.html, data.html, add.html, analysis.html
static/ style.css and the chart imagesStart app.py with the code that reads your data:
import csv
from datetime import date
from statistics import mean, median, mode
from flask import Flask, render_template, request
app = Flask(__name__)
DATA = "data/wellbeing.csv"
FIELDS = ["id", "year_group", "survey_date", "sleep_hours",
"study_hours", "exercise_mins", "plays_sport", "mood"]
def load_records():
with open(DATA, newline="") as f:
return list(csv.DictReader(f))
@app.route("/")
def home():
return render_template("index.html")Run it with python app.py and open http://127.0.0.1:5000 in a browser. The site runs on your own computer; it does not need the internet.
load_records() opens the CSV and returns a list of records. Each record is a dictionary, so r["sleep_hours"] is that record's sleep. Every page that needs data calls it, so the website always reads the file directly (AR1).
A template turns the records into a table. This is templates/data.html:
<h1>Survey records</h1>
<form method="get" action="/data">
<label for="year">Year group (1 to 6):</label>
<input type="number" id="year" name="year" min="1" max="6" value="{{ year }}">
<button type="submit">Filter</button>
</form>
<p>{{ records|length }} records shown.</p>
<table>
<tr><th>ID</th><th>Year</th><th>Date</th><th>Sleep</th><th>Mood</th></tr>
{% for r in records %}
<tr><td>{{ r.id }}</td><td>{{ r.year_group }}</td><td>{{ r.survey_date }}</td>
<td>{{ r.sleep_hours }}</td><td>{{ r.mood }}</td></tr>
{% endfor %}
</table>{% for r in records %} repeats the table row once for every record. {{ records|length }} shows how many there are.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.