Major refactor

Refactor the templates to include a header, use csv-metadata-quality
in a subshell instead of trying to import and pass args to it (which
I actually managed to do, but then trying to capture the output was
an issue), and use ansi2html to prepare the CLI output as the HTML.
This commit is contained in:
2021-03-12 19:14:49 +02:00
parent ed747b2cef
commit e13d63bf6b
6 changed files with 50 additions and 6 deletions

29
main.py
View File

@ -1,5 +1,8 @@
import os
import subprocess
import sys
from ansi2html import Ansi2HTMLConverter
from csv_metadata_quality.version import VERSION as cli_version
from flask import Flask, abort, redirect, render_template, request, url_for
from werkzeug.utils import secure_filename
@ -32,9 +35,33 @@ def upload_file():
return "No file selected"
# TODO: probably use a base64- and URL-encoded version of the filename here so
# we can allow results to be saved and shared?
@app.route("/process/<filename>")
def process_file(filename):
return render_template("process.html", cli_version=cli_version, filename=filename)
# do we need to use secure_filename again here?
input_file = os.path.join(app.config["UPLOAD_PATH"], filename)
# TODO: write an output file based on the input file name
output_file = os.path.join(app.config["UPLOAD_PATH"], "test.csv")
sys.argv = ["", "-i", input_file, "-o", output_file]
# run subprocess and capture output as UTF-8 so we get a string instead of
# bytes for ansi2html
results = subprocess.run(
["csv-metadata-quality", "-i", input_file, "-o", output_file],
capture_output=True,
encoding="UTF-8",
)
# convert the output to HTML using ansi2html
conv = Ansi2HTMLConverter()
html = conv.convert(results.stdout)
return render_template(
"process.html", cli_version=cli_version, filename=filename, stdout=html
)
# I should remember this Flask-specific way to send files to the client
# return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
if __name__ == "__main__":