今回はSqliteとFlaskを一緒に使うことにします。
HPでは正式?なやり方のってますがこっちはもっとベタなやり方をします。
http://flask.pocoo.org/docs/0.12/patterns/sqlite3/
from flask import Flask,render_template from flask_httpauth import HTTPBasicAuth import sqlite3 app=Flask(__name__) auth=HTTPBasicAuth() users={ "root":"root", "oper":"1234" } @auth.get_password def get_pw(username): if username in users: return users.get(username) return None @app.route("/") @auth.login_required def index(): conn=sqlite3.connect('db1.db') c=conn.cursor() c.execute("select * from stocks") data=c.fetchall() print(data) c.close() conn.close() return render_template("index.html",data = data) if __name__=='__main__': app.run(port=5001)
そして、Index.htmlにJinja2を使ってLoopingします。
{% for %} ~~{% endfor %}
<html lang="en"> <head></head> <body> <div class="container"> <div class="header"> <h3>data</h3> <div class = "table-responsive"> <table class = "table table-bordered table-hover"> <tr> <th>date</th> <th>trans</th> <th>Symbol</th> </tr> {% for d in data %} <tr> {% for d1 in d %} <td>{{d1}}</td> {% endfor %} </tr> {% endfor %} </table> </div> </div> </div> </body> </html>