price_flask.py #1 完成版
Flaskを始めたので詰まった所や学んだことをどんどん垂れ流していきます。 最初から1つ1つ紐解いていきます。
from flask import Flask, render_template, request import sqlite3 as sql import math app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') @app.route('/price') def list(): con = sql.connect("price.db") con.row_factory = sql.Row cur = con.cursor() # total cur.execute("select count(*) from event") result = cur.fetchall() total = result[0][0] # 表示件数 (1ページに表示する件数) length = 10 # ページ数 (存在する) pages = math.floor(total / length) + 1 # クエストリングにページ数を渡す。 # pageを指定しない場合=0, つまり先頭を表示する。 page = request.args.get("page", default=1, type=int) page = 1 if page < 1 else page page = pages if page > pages else page offset = (page - 1) * length limit = length # 指定の範囲を抽出 cur.execute("select *, strftime('%Y-%m-%d %H:%M:%S', cast( Timestamp as BIGINT), 'unixepoch') as datetime from event limit {0} offset {1}".format(limit, offset)) rows = cur.fetchall() # 指定の範囲を抽出 cur.execute("select *, strftime('%Y-%m-%d %H:%M:%S', cast( Timestamp as BIGINT), 'unixepoch') as datetime from event") all_rows = cur.fetchall() pagination = { "page": page, "length":length, "pages": pages, "total": total } return render_template("price_table.html",rows = rows,all_rows = all_rows, pagination=pagination) if __name__ == '__main__': app.run(debug = True)