計算機科学のブログ

ほしい物リスト

Python - Flask - Building a Webapp: Web Development - templates

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 7.(Building a Webapp: Web Development)、EXERCISE(366/682)の解答を求めてみる。

コード

webapp/app.py

import os

from flask import Flask, render_template

import swimclub

app = Flask(__name__)


@app.get('/')
def index():
    return render_template(
        'index.html.j2', title='Welcome to the Swimclub system'
    )


@app.get('/swimmers')
def display_swimmers():
    swimmers = []
    files = os.listdir(swimclub.FOLDER)
    files.remove('.DS_Store')
    for file in files:
        swimmer, *_ = swimclub.read_swim_data(file)
        if swimmer not in swimmers:
            swimmers.append(swimmer)
    return sorted(swimmers)


if __name__ == '__main__':
    app.run()