計算機科学のブログ

ほしい物リスト

Python - SQLite - List Comprehensions: Database Integrations - session, form, request

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 13.(List Comprehensions: Database Integrations)、EXERCISE(611/682)の解答を求めてみる。

コード

webapp/app.py

# import os


from flask import Flask, render_template, request, session

import data_utils

app = Flask(__name__)
app.secret_key = 'You will never guess'


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


@app.get('/swims')
def display_swim_sessions():
    data = data_utils.get_swim_sessions()
    dates = [session[0].split(' ')[0] for session in data]
    return render_template(
        'select.html.j2',
        title='Select a swim session',
        url='/swimmers',
        select_id='chosen_date',
        data=dates,
    )


@app.post('/swimmers')
def display_swimmers():
    session['chosen_date'] = request.form['chosen_date']
    data = data_utils.get_session_swimmers(session['chosen_date'])
    swimmers = [f'{name}-{age}' for name, age in data]
    return render_template(
        'select.html.j2',
        title='Select a swimmer',
        url='/showfiles',
        select_id='swimmer',
        data=sorted(swimmers),
    )


@app.post('/showfiles')
def display_swimmer_files():
    # populate_data()
    name = request.form['name']
    return render_template(
        'select.html.j2',
        title='Select an event',
        url='/showbarchart',
        select_id='file',
        data=session['swimmers'][name],
    )


@app.post('/showbarchart')
def show_bar_chart():
    file_id = request.form['file']
    # location = swimclub.produce_bar_chart(file_id, 'templates/')
    # return render_template(location.splot('/')[-1])
    return ''


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