diff --git a/app/__init__.py b/app/__init__.py index 0a1725a407c7dbc81706fa4fbc72c83fc6c18147..71c6ee7e0970ac6a6dea8fa8cde8bf4781bd880e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -68,10 +68,6 @@ def get_locale(): """ -# Run flask app with socketIO -socketio = SocketIO(app, cors_allowed_origins="*") -# socketio = SocketIO() -socketio.init_app(app) #mariabd mysql portti 3306 tarkista? @@ -82,6 +78,11 @@ migrate = Migrate(app, db) login = LoginManager(app) login.login_view = 'login' +# Run flask app with socketIO +socketio = SocketIO(app, cors_allowed_origins="*") +# socketio = SocketIO() +socketio.init_app(app) + # Register blueprints from .task.views import task_blueprint from .experiment.views import experiment_blueprint diff --git a/app/experiment/views.py b/app/experiment/views.py index 46d148c0e0561adadcc26c7ed69208b05dc894a2..241ecb682a59557971c67c1e378d70f8e2de8f28 100644 --- a/app/experiment/views.py +++ b/app/experiment/views.py @@ -2,6 +2,7 @@ import os import secrets from datetime import date +from tempfile import mkstemp from flask_socketio import emit from sqlalchemy import and_ @@ -13,7 +14,8 @@ from flask import ( flash, redirect, url_for, - Blueprint + Blueprint, + send_file ) from app import app, db, socketio @@ -1045,7 +1047,7 @@ def remove_rows(rows): @socketio.on('connect', namespace="/create_embody") -def create_embody(): +def start_create_embody(): emit('success', {'connection': 'on'}) @@ -1059,47 +1061,39 @@ def create_embody(meta): @socketio.on('end', namespace="/create_embody") -def create_embody(): - emit('end', {'connection': 'off'}) - - +def end_create_embody(): + db.session.close() @socketio.on('connect', namespace="/download_csv") -def create_embody(): +def start_download_csv(): emit('success', {'connection': 'Start generating CSV file'}) - -from tempfile import mkstemp -from flask import send_file - @socketio.on('generate_csv', namespace="/download_csv") -def create_embody(meta): - +def process_download_csv(meta): exp_id = meta["exp_id"] data = generate_csv(exp_id) + # error handling if isinstance(data, Exception): emit('timeout', {'exc': str(data)}) return - filename = "experiment_{}_{}".format( - exp_id, date.today().strftime("%Y-%m-%d")) - + # create temporary file fd, path = mkstemp() - - print(fd) - print(path) - with os.fdopen(fd, 'w') as tmp: tmp.write(data) tmp.flush() + # return path and filename to front so user can start downloading + filename = "experiment_{}_{}".format( + exp_id, date.today().strftime("%Y-%m-%d")) path = path.split('/')[-1] - emit('file_ready', {'path': path, 'filename': filename}) - # return saved_data_as_file(filename, csv) +@socketio.on('end', namespace="/download_csv") +def end_download_csv(): + db.session.close() diff --git a/app/static/js/getCSV.js b/app/static/js/getCSV.js index f8416028ed65dd460a5f64653fa78a8094cdfe17..9c20c7631107fe028b1b310203a26253d828bae3 100644 --- a/app/static/js/getCSV.js +++ b/app/static/js/getCSV.js @@ -26,6 +26,7 @@ $(document).ready(function() { socket.on('timeout', function(data) { // kill connection + socket.emit('end') socket.disconnect() exportButton.text('Export results') diff --git a/app/static/js/getDrawing.js b/app/static/js/getDrawing.js index 98d80c0bcbd2244a4a60def4f76c4e2041ff3cbc..70db2ae5142c7cda61360b362877a3b2d410a20d 100644 --- a/app/static/js/getDrawing.js +++ b/app/static/js/getDrawing.js @@ -19,6 +19,8 @@ $(document).ready(function() { }); socket.on('end', function(img) { + // kill connection + socket.emit('end') socket.disconnect() // Draw image to statistic -page diff --git a/embody_plot.py b/embody_plot.py index 1c8666da31b11c4336dbf049b8f27d98eae2f8bb..f540d77fafee5a5fbdf446d50d2732bc08b4be4d 100644 --- a/embody_plot.py +++ b/embody_plot.py @@ -124,6 +124,8 @@ def timeit(method): def get_coordinates(idpage, idembody=None, select_clause=SELECT_BY_PAGE_AND_PICTURE): """Select all drawn points from certain stimulus and plot them onto the human body""" + + # init db db = MyDB() db.query(select_clause, (idpage,idembody)) @@ -141,6 +143,9 @@ def get_coordinates(idpage, idembody=None, select_clause=SELECT_BY_PAGE_AND_PICT else: plt = plot_coordinates(coordinates, DEFAULT_IMAGE_PATH) + # close db connection + db.__del__() + # Save image to ./app/static/ img_filename = 'PAGE-' + str(idpage) + '-' + DATE_STRING + '.png' plt.savefig(STATIC_PATH + img_filename)