From b18d9ccfb3e5b15dab608a5acfea71e61808c97a Mon Sep 17 00:00:00 2001 From: Hendrik Borras Date: Sun, 12 Nov 2017 17:28:59 +0100 Subject: [PATCH] Implemented CSV export of the database --- README.md | 2 +- frontend/templates/settings.html | 25 +++++++++++++++++++++ frontend/web_ui.py | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 frontend/templates/settings.html diff --git a/README.md b/README.md index 46a27c6..699370c 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ This behaviour is very much open to discussion. - [x] Detector readout - [x] WebUI - [x] Hotspot -- [ ] Interface for getting the raw data and database dumps +- [x] Interface for getting the raw data and database dumps - [x] Interface to create custom plots - [x] Include about page - [ ] Add useful configurations to the config file diff --git a/frontend/templates/settings.html b/frontend/templates/settings.html new file mode 100644 index 0000000..6dbbe19 --- /dev/null +++ b/frontend/templates/settings.html @@ -0,0 +1,25 @@ +{% extends "cosmic_base.html" %} +{% block title %}Settings{% endblock %} +{% block head_additions %} +{% endblock %} +{% block content %} +

Settings

+ +
+
+
+ CSV export +
+ +
+ Here you can export the latest events in CSV format.
+
+ +
+
+
+
+ +
+ +{% endblock %} \ No newline at end of file diff --git a/frontend/web_ui.py b/frontend/web_ui.py index 70da079..2ebe3a5 100644 --- a/frontend/web_ui.py +++ b/frontend/web_ui.py @@ -6,10 +6,12 @@ import io import sqlite3 import matplotlib.dates as mdates from flask_googlemaps import GoogleMaps +import csv app = Flask(__name__) sqlite_location = "../storage/sqlite_db" + def initDB(): conn = sqlite3.connect(sqlite_location, timeout=60.0) cursor = conn.cursor() @@ -74,6 +76,7 @@ def dashboard_page(): # get collumn names cursor.execute("PRAGMA table_info(Events);") col_names = cursor.fetchall() + conn.close() # extract data for i in range(0,len(col_names)): @@ -101,6 +104,7 @@ def plotting_page(): # get collumn names cursor.execute("PRAGMA table_info(Events);") col_names = cursor.fetchall() + conn.close() # extract data for i in range(0,len(col_names)): @@ -117,6 +121,38 @@ def plotting_page(): return render_template('plotting.html', location_vars=location_vars) +@app.route('/settings/', methods=['GET', 'POST']) +def settings_page(): + return render_template('settings.html') + + +@app.route('/CosmicPi_data.csv', methods=['GET']) +def csv_export(): + conn = sqlite3.connect(sqlite_location, timeout=60.0) + cursor = conn.cursor() + + # get collumn names + cursor.execute("PRAGMA table_info(Events);") + col_data = cursor.fetchall() + col_names = [] + for i in range(0, len(col_data)): + col_names.append(col_data[i][1]) + + # get data from db + cursor.execute("SELECT * FROM Events ORDER BY UTCUnixTime DESC, SubSeconds DESC;") + data = cursor.fetchall() + conn.close() + + # write CSV export to memory + output = io.BytesIO() + writer = csv.writer(output) + writer.writerow(col_names) + writer.writerows(data) + + response = make_response(output.getvalue()) + response.headers['Content-Type'] = 'text/csv' + return response + @app.route('/about/', methods=['GET', 'POST']) def about_page(): @@ -146,6 +182,7 @@ def build_plot(): cursor.execute("SELECT * FROM Events WHERE UTCUnixTime BETWEEN ? AND ? ORDER BY UTCUnixTime DESC, SubSeconds DESC;", (start_time, end_time)) data = cursor.fetchall() + conn.close() # massage data if len(data) == 0: