mirror of
https://github.com/ilri/csv-metadata-quality-web.git
synced 2024-11-26 16:38:21 +01:00
Add downloading of cleaned CSV file
This commit is contained in:
parent
37cee1d3f0
commit
69abc3d764
31
main.py
31
main.py
@ -5,7 +5,15 @@ from base64 import b64decode, b64encode
|
|||||||
|
|
||||||
from ansi2html import Ansi2HTMLConverter
|
from ansi2html import Ansi2HTMLConverter
|
||||||
from csv_metadata_quality.version import VERSION as cli_version
|
from csv_metadata_quality.version import VERSION as cli_version
|
||||||
from flask import Flask, abort, redirect, render_template, request, url_for
|
from flask import (
|
||||||
|
Flask,
|
||||||
|
abort,
|
||||||
|
redirect,
|
||||||
|
render_template,
|
||||||
|
request,
|
||||||
|
send_from_directory,
|
||||||
|
url_for,
|
||||||
|
)
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -46,8 +54,10 @@ def process_file(base64slug):
|
|||||||
|
|
||||||
# do we need to use secure_filename again here?
|
# do we need to use secure_filename again here?
|
||||||
input_file = os.path.join(app.config["UPLOAD_PATH"], filename)
|
input_file = os.path.join(app.config["UPLOAD_PATH"], filename)
|
||||||
# TODO: write an output file based on the input file name
|
# write output file with the same name as the input file plus "-cleaned"
|
||||||
output_file = os.path.join(app.config["UPLOAD_PATH"], "test.csv")
|
output_file = os.path.join(
|
||||||
|
app.config["UPLOAD_PATH"], os.path.splitext(filename)[0] + "-cleaned.csv"
|
||||||
|
)
|
||||||
|
|
||||||
sys.argv = ["", "-i", input_file, "-o", output_file]
|
sys.argv = ["", "-i", input_file, "-o", output_file]
|
||||||
|
|
||||||
@ -62,11 +72,20 @@ def process_file(base64slug):
|
|||||||
conv = Ansi2HTMLConverter()
|
conv = Ansi2HTMLConverter()
|
||||||
html = conv.convert(results.stdout)
|
html = conv.convert(results.stdout)
|
||||||
return render_template(
|
return render_template(
|
||||||
"result.html", cli_version=cli_version, filename=filename, stdout=html
|
"result.html",
|
||||||
|
cli_version=cli_version,
|
||||||
|
filename=filename,
|
||||||
|
stdout=html,
|
||||||
|
base64name=base64slug,
|
||||||
)
|
)
|
||||||
|
|
||||||
# I should remember this Flask-specific way to send files to the client
|
|
||||||
# return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
|
@app.route("/result/<base64slug>/download")
|
||||||
|
def result_download(base64slug):
|
||||||
|
filename = b64decode(base64slug).decode("ascii")
|
||||||
|
filename = secure_filename(os.path.splitext(filename)[0] + "-cleaned.csv")
|
||||||
|
|
||||||
|
return send_from_directory(app.config["UPLOAD_PATH"], filename, as_attachment=True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<main class="flex-shrink-0">
|
<main class="flex-shrink-0">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="mt-3 fs-2">Results</h1>
|
<h1 class="mt-3 fs-2">Results</h1>
|
||||||
<p>Processing <code>{{ filename }}</code>.</p>
|
<p>Processing <code>{{ filename }}</code>. Download <a href="/result/{{ base64name }}/download" title="{{ filename | replace('.csv', '-cleaned.csv') }}">cleaned file</a>.</p>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user