計算機科学のブログ

ほしい物リスト

Python - Building a Webapp: Web Development - Flask

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(21/124)の解答を求めてみる。

コード

webapp/app.py

import os

from flask import Flask

import swimclub

app = Flask(__name__)


@app.get('/')
def index():
    return "This is a placeholder for your webapp's opening page."


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


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

入出力結果(Terminal, Zsh)

%  .../webapp/
app.py 
 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [21/Apr/2025 16:07:47] "GET /swimmers HTTP/1.1" 200 -
^C%                                                                                                        
%

HTML

/swimmers

["Hannah", "Darius", "Owen", "Mike", "Abi", "Ruth", "Tasmin", "Erika", "Maria", "Elba", "Ali", "Chris", "Aurora",
"Katie", "Alison", "Emma", "Calvin", "Blake", "Bill", "Dave", "Lizzie", "Carl"]