"Hello world" PDF report.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 28 Jan 2019 20:56:38 +0000 (21:56 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 28 Jan 2019 20:56:38 +0000 (21:56 +0100)
web/seepark_web.py

index d9c61c3..acec9c5 100644 (file)
@@ -4,7 +4,7 @@ import configparser
 import os
 import sys
 from collections import defaultdict
-from flask import Flask, render_template, jsonify, request, abort, Response
+from flask import Flask, render_template, jsonify, request, abort, Response, make_response
 import flask.json
 from flask_sqlalchemy import SQLAlchemy, inspect
 from sqlalchemy import func
@@ -275,6 +275,27 @@ def currentwater(version):
     return jsonify({"value": value, "timestamp": timestamp})
 
 
+@app.route('/report/<int:year>-<int:month>')
+def report(year, month):
+    import io
+    import numpy as np
+    import matplotlib.pyplot as plt
+    from matplotlib.backends.backend_pdf import PdfPages
+
+    binary_pdf = io.BytesIO()
+    with PdfPages(binary_pdf) as pdf:
+        a4 = (21./2.54, 29.7/2.54)
+        plt.figure(figsize=a4)
+        x = np.arange(100)
+        y = np.sin(x/4)
+        plt.plot(x, y)
+        pdf.savefig()
+    response = make_response(binary_pdf.getvalue())
+    response.headers['Content-Type'] = 'application/pdf'
+    response.headers['Content-Disposition'] = 'attachment; filename=seepark_{:04d}-{:02d}.pdf'.format(year, month)
+    return response
+
+
 @app.route("/")
 def index():
     airvalue, airtime     = currentairtemperature(cityid)