diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2019-09-05 14:46:34 +0100 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2019-09-05 14:46:34 +0100 |
| commit | 6a83509040f6222ae330ad796beb0104d85313a7 (patch) | |
| tree | e61de30fc67d717ff4303a05e16f2d262948f30c /app | |
| parent | Remove time axis warning in console (diff) | |
| download | AnnotateChange-6a83509040f6222ae330ad796beb0104d85313a7.tar.gz AnnotateChange-6a83509040f6222ae330ad796beb0104d85313a7.zip | |
Add ability to download annotations as CSV file
Diffstat (limited to 'app')
| -rw-r--r-- | app/admin/routes.py | 51 | ||||
| -rw-r--r-- | app/templates/admin/annotations.html | 3 |
2 files changed, 52 insertions, 2 deletions
diff --git a/app/admin/routes.py b/app/admin/routes.py index 0736549..518b3fc 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -1,8 +1,18 @@ # -*- coding: utf-8 -*- +import clevercsv +import io import os - -from flask import render_template, flash, redirect, url_for, current_app +import datetime + +from flask import ( + current_app, + flash, + redirect, + render_template, + send_file, + url_for, +) from werkzeug.utils import secure_filename @@ -254,6 +264,43 @@ def view_annotations(): ) +@bp.route("/annotations/download", methods=("GET",)) +@admin_required +def download_annotations_csv(): + annotations = ( + Annotation.query.join(Task, Annotation.task) + .join(User, Task.user) + .join(Dataset, Task.dataset) + .order_by(Dataset.id, User.username, Annotation.cp_index) + .all() + ) + + # based on: https://stackoverflow.com/a/45111660/1154005 + header = ["DatasetID", "DatasetName", "UserID", "AnnotationIndex"] + + proxy = io.StringIO() + writer = clevercsv.writer(proxy) + writer.writerow(header) + for ann in annotations: + row = [ann.task.dataset.id, ann.task.dataset.name, ann.task.user.id] + if ann.cp_index is None: + row.append("no_cp") + else: + row.append(ann.cp_index) + writer.writerow(row) + + mem = io.BytesIO() + mem.write(proxy.getvalue().encode("utf-8")) + mem.seek(0) + proxy.close() + + fname = "%i_annotations.csv" % (round(datetime.datetime.now().timestamp())) + + return send_file( + mem, as_attachment=True, attachment_filename=fname, mimetype="text/csv" + ) + + @bp.route("/annotations_by_dataset/<int:dset_id>", methods=("GET",)) @admin_required def view_annotations_by_dataset(dset_id): diff --git a/app/templates/admin/annotations.html b/app/templates/admin/annotations.html index 5a3f66a..883fd70 100644 --- a/app/templates/admin/annotations.html +++ b/app/templates/admin/annotations.html @@ -15,6 +15,9 @@ </div> <br> <article class="overview"> + <a href="./annotations/download">Download as CSV</a> + <br> + <br> <table id="annotations-table" class="table table-striped"> <thead class="thead-dark"> <th scope="col">Dataset</th> |
