diff --git a/.gitignore b/.gitignore index 9d95958..b6e4761 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,3 @@ -# own -.pre-commit-config.yaml -.vs - # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ebe0086 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM docker.io/python:3.8 + +RUN apt-get -y update && apt-get install -y apt-utils gcc g++ +RUN apt-get -y upgrade +RUN git clone https://github.com/Mat-O-Lab/KnowledgeUI /src +RUN pip install -r /src/requirements.txt +WORKDIR /src + +CMD ["gunicorn", "-b", "0.0.0.0:5000", "wsgi:app", "--workers=3"] diff --git a/README.md b/README.md deleted file mode 100644 index f36c897..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# OmeroExtractor -Tool to extract Meta Data from Omero Server diff --git a/app.py b/app.py new file mode 100644 index 0000000..d5cd1bc --- /dev/null +++ b/app.py @@ -0,0 +1,126 @@ +import os +import base64 + +from flask import Flask, flash, request, jsonify, render_template +from flask_swagger_ui import get_swaggerui_blueprint +from flask_wtf import FlaskForm +from flask_bootstrap import Bootstrap + +from wtforms import URLField, SelectField +from wtforms.validators import DataRequired + +from config import config +#from annotator import CSV_Annotator + +config_name = os.environ.get("APP_MODE") or "development" + +app = Flask(__name__) +app.config.from_object(config[config_name]) + +bootstrap = Bootstrap(app) + + +SWAGGER_URL = "/api/docs" +API_URL = "/static/swagger.json" +swaggerui_blueprint = get_swaggerui_blueprint( + SWAGGER_URL, + API_URL, + config={ + "app_name": "CSVtoCSVW" + } +) + +app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL) +separators = ["auto", ";", ",", "\\t", "\\t+", + "|", "\s+", "\s+|\\t+|\s+\\t+|\\t+\s+"] +encodings = ['auto', 'ISO-8859-1', 'UTF-8', 'ascii', 'latin-1', 'cp273'] + + +class StartForm(FlaskForm): + data_url = URLField( + 'URL Data File', + validators=[DataRequired()], + description='Paste URL to a data file, e.g. csv, TRA' + ) + separator_sel = SelectField( + 'Choose Separator, default: auto detect', + choices=separators, + description='select a separator for your data manually', + default='auto' + ) + encoding_sel = SelectField( + 'Choose Encoding, default: auto detect', + choices=encodings, + description='select an encoding for your data manually', + default='auto' + ) + + +@app.route('/', methods=['GET', 'POST']) +def index(): + logo = './static/resources/MatOLab-Logo.svg' + start_form = StartForm() + message = '' + result = '' + return render_template( + "index.html", + logo=logo, + start_form=start_form, + message=message, + result=result + ) + + +@app.route('/create_annotator', methods=['POST']) +def create_annotator(): + logo = './static/resources/MatOLab-Logo.svg' + start_form = StartForm() + message = '' + result = '' + + if start_form.validate_on_submit(): + annotator = CSV_Annotator( + separator=start_form.separator_sel.data, + encoding=start_form.encoding_sel.data + ) + + try: + meta_file_name, result = annotator.process( + start_form.data_url.data) + except (ValueError, TypeError) as error: + flash(str(error)) + else: + b64 = base64.b64encode(result.encode()) + payload = b64.decode() + + return render_template( + "index.html", + logo=logo, + start_form=start_form, + message=message, + result=result, + payload=payload, + filename=meta_file_name + ) + return render_template( + "index.html", + logo=logo, + start_form=start_form, + message=message, + result=result + ) + + +@app.route("/api", methods=["GET", "POST"]) +def api(): + if request.method == "POST": + content = request.get_json() + annotator = CSV_Annotator( + encoding=content['encoding'], separator=content['separator']) + filename, file_data = annotator.process(content['data_url']) + return jsonify({"filename": filename, "filedata": file_data}) + + +if __name__ == "__main__": + port = int(os.environ.get("PORT", 5000)) + app.run(host="0.0.0.0", port=port, debug=app.config["DEBUG"]) diff --git a/config.py b/config.py new file mode 100644 index 0000000..a8fa07e --- /dev/null +++ b/config.py @@ -0,0 +1,18 @@ +import os + +class Config: + SECRET_KEY = os.environ.get("SECRET_KEY") or "harder to guess string" + TEMPORAL_FOLDER = os.environ.get("TEMPORAL_FOLDER") or "tmp" + TEMPLATES_AUTORELOAD = True + +class DevelopmentConfig(Config): + DEBUG = True + +class ProductionConfig(Config): + DEBUG = False + +config = { + "development": DevelopmentConfig, + "production": ProductionConfig, + "default": DevelopmentConfig +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d2230ae --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: "3" +services: + knowledgeui: + build: . + container_name: knowledgeui + volumes: + - .:/src + ports: + - 5000:5000 + restart: always + networks: + - knowledgeui_net + +networks: + knowledgeui_net: + name: knowledgeui + driver: bridge diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..70bf2f8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +gunicorn +chardet +rdflib +flask +flask-bootstrap diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..f843cf6 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,58 @@ + +
+
+

CSVtoCSVW

+

KnowledgeUI

+

+ Tool for mapping data files to json-ld metadata. + Mat-o-Lab Demonstrator App for the aluminium use case. +

+
+
+ {{ start_form.csrf_token }} + {{ start_form.data_url.label }} + {{ start_form.data_url(class="form-control", **{'aria-describedby': 'urlHelp'}) }} +
{{ start_form.data_url.description }}
+
+ {% if start_form.data_url.errors %} +
    + {% for error in start_form.data_url.errors %} +
  • {{ error }}
  • + {% endfor %} +
+ {% endif %} +
+ {{ start_form.separator_sel.label }} + {{ start_form.separator_sel(class="form-control", **{'aria-describedby': 'sep_help'}) }} +
{{ start_form.separator_sel.description }}
+ + {{ start_form.encoding_sel.label }} + {{ start_form.encoding_sel(class="form-control", **{'aria-describedby': 'enc_help'}) }} +
{{ start_form.encoding_sel.description }}
+ +
+ +
+ + {% block tab %} + {% endblock %} +
+ {% with messages = get_flashed_messages() %} +
+@@ -75,10 +50,13 @@
Result
+
+ {% endwith %} +
+{% block footer %} + + +{% endblock %} + + +{% block scripts %} + + +{% endblock %}