[PATCH coffee-flask] Add graphs with total coffee consumption on login screen

--- app.py | 37 +++++++++++++++++++++++++++++++++++-- coffee_db.py | 25 ++++++++++++++++++++++++- templates/user.html | 5 ++++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 807f614..a597ad6 100644 --- a/app.py +++ b/app.py @@ -78,7 +78,35 @@ def coffee_graph_flavors(): ax.set_aspect(1) ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '') ax.legend(flavors, bbox_to_anchor=(1.0, 1.0)) - ax.set_title("Your taste") + + if "uid" in session: + ax.set_title("Your taste") + else: + ax.set_title("Team's taste") + + fig.savefig(b, format="svg", bbox_inches="tight") + b.seek(0) + return send_file(b, mimetype="image/svg+xml") + +@app.route("/coffee/graph_flavors_history") +def coffee_graph_flavors_history(): + b = BytesIO() + if "uid" in session: + uid = session["uid"] + flavors, counts = zip(*db.coffee_flavors_history(uid)) + else: + flavors, counts = zip(*db.coffee_flavors_history()) + fig = plt.figure(figsize=(3, 3)) + ax = fig.add_subplot(111) + ax.set_aspect(1) + ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '') + ax.legend(flavors, bbox_to_anchor=(1.0, 1.0)) + + if "uid" in session: + ax.set_title("Your taste") + else: + ax.set_title("This week taste") + fig.savefig(b, format="svg", bbox_inches="tight") b.seek(0) return send_file(b, mimetype="image/svg+xml") @@ -133,7 +161,12 @@ def coffee_graph_history(): xdays[-2] = "YDA" ax.set_xticks(range(len(unix_days))) ax.set_xticklabels(xdays) - ax.set_title("Your week") + + if "uid" in session: + ax.set_title("Your week") + else: + ax.set_title("This week total") + ax.yaxis.set_major_locator(MaxNLocator(integer=True)) fig.savefig(b, format="svg", bbox_inches="tight") b.seek(0) diff --git a/coffee_db.py b/coffee_db.py index 8c18e63..be87c13 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -89,6 +89,28 @@ def coffee_flavors(uid=None): close_db(conn) return res +def coffee_flavors_history(uid=None): + conn, c = open_db() + + if uid is None: + res = list(c.execute(""" + select f.name, count(c.flavor) from + (select num,date('now',-num || ' days') as d from days) ds + left join flavors f + left join (select * from coffees) c + on f.name=c.flavor and d = date(c.time) group by f.name + """)) + else: + res = list(c.execute(""" + select f.name, count(c.flavor) from + (select num,date('now',-num || ' days') as d from days) ds + left join flavors f + left join (select * from coffees where id = ?) c + on f.name=c.flavor and d = date(c.time) group by f.name + """, (uid,))) + + close_db(conn) + return res def coffee_history(uid=None): conn, c = open_db() @@ -97,7 +119,8 @@ def coffee_history(uid=None): res = list(c.execute(""" select strftime('%s', ds.d),count(c.flavor),c.flavor from (select num,date('now',-num || ' days') as d from days) ds - left join coffees c + left join + (select time,flavor from coffees) c on d = date(c.time) group by d, c.flavor """)) else: diff --git a/templates/user.html b/templates/user.html index 95ee95e..51a41f6 100644 --- a/templates/user.html +++ b/templates/user.html @@ -30,5 +30,8 @@ </form> </p {% else %} - Use your card/token to log in... + <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}"> + <img src="{{ url_for('coffee_graph_flavors_history', _external=True, stamp=stamp) }}"> + + <p>Use your card/token to log in...</p> {% endif %} -- 2.7.4
participants (1)
-
Jaroslav Klapalek