from json import loads
from requests import post
import jinja2
+import threading
matplotlib.use('Agg')
CORS(app, supports_credentials=True)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
+# Workaround for
+# https://github.com/matplotlib/matplotlib/issues/13723. Despite the
+# issue is closed, the problem is still there. See
+# https://github.com/matplotlib/matplotlib/issues/13723#issuecomment-761302131.
+plot_lock = threading.Lock()
# Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
# updated to our needs
@app.route('/')
def hello():
- if "uid" in session:
- uid = session["uid"]
- return render_template('hello.html', name=db.get_name(uid))
return render_template('hello.html')
+@app.route('/home')
+def home():
+ # TODO: Replace stamp parameter with proper cache control HTTP
+ # headers in response
+ return render_template('home.html',
+ stamp=time.time(),
+ last_events=db.last_events())
+
+
@app.route('/login', methods=["POST"])
@app.route('/login/<iid>')
def login(iid=None):
def logout():
session.pop('uid', None)
session.pop('iid', None)
- return redirect(url_for('user'))
+ return redirect(url_for('home'))
@app.route('/user')
stamp=time.time(),
last_events=db.last_events()
)
- # TODO: Replace stamp parameter with proper cache control HTTP
- # headers in response
- return render_template('user.html', stamp=time.time(),
- last_events=db.last_events(),
- )
+ return redirect(url_for('home'))
@app.route('/user/rename')
if "normalize" in matplotlib.pyplot.pie.__code__.co_varnames:
# Matplotlib >= 3.3.0
ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '',
- normalize=True)
+ normalize=sum(counts) != 0)
else:
# Matplotlib < 3.3.0
ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
else:
ax.set_title("This week taste")
- fig.savefig(b, format="svg", bbox_inches="tight")
+ with plot_lock:
+ fig.savefig(b, format="svg", bbox_inches="tight")
+ plt.close(fig)
b.seek(0)
return send_file(b, mimetype="image/svg+xml")
ax.set_title("This week total")
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
- fig.savefig(b, format="svg", bbox_inches="tight")
+ with plot_lock:
+ fig.savefig(b, format="svg", bbox_inches="tight")
+ plt.close(fig)
b.seek(0)
- plt.close(fig)
return send_file(b, mimetype="image/svg+xml")
return redirect(url_for('user'))
-# TODO: Remove me - unused
-@app.route("/coffee/count")
-def coffee_count():
- start = request.args.get("start")
- stop = request.args.get("stop")
- return str(dict(db.drink_count(session.get("uid"), start, stop)).get("coffee", 0))
-
-
@app.route('/js')
def js():
response = make_response(render_template('main.js'))