File Upload Example
+Upload File/Enter Text
Converted Text
-{{ text }}
+ {{ resp_text }}
diff --git a/.vscode/settings.json b/.vscode/settings.json index 55f96d6..65293fd 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "[python]": { - "editor.defaultFormatter": "ms-python.black-formatter" + "editor.defaultFormatter": "ms-python.autopep8" }, "python.formatting.provider": "none" } \ No newline at end of file diff --git a/DOCKERFILE b/DOCKERFILE deleted file mode 100755 index a27e2d6..0000000 --- a/DOCKERFILE +++ /dev/null @@ -1,17 +0,0 @@ -FROM python:3.11.3-alpine -WORKDIR /code - -RUN apk --update --upgrade add --no-cache gcc musl-dev jpeg-dev zlib-dev libffi-dev cairo-dev pango-dev gdk-pixbuf-dev - -RUN python -m pip install --upgrade pip -COPY requirements.txt requirements.txt -RUN pip install -r requirements.txt - - -#set Enviroment Variables used in FLASK -ENV FLASK_APP=app -ENV FLASK_RUN_HOST=0.0.0.0 -ENV FLASK_RUN_PORT=8080 -EXPOSE 8080 - -CMD [ "flask", "run" ] diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..c6faf50 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM alpine:3.18.4 +RUN apk add --no-cache python3 py3-pip +COPY requirements.txt /home +RUN pip install -r /home/requirements.txt +COPY src webserver +WORKDIR /webserver + +ENV FLASK_DEGUB=false +ENTRYPOINT [ "python3", "-u", "main.py" ] +EXPOSE 5000 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b97bed9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.0' +services: + web: + build: . + image: anki_convert + container_name: anki_convert + restart: unless-stopped + ports: + - 8080:5000 \ No newline at end of file diff --git a/dockerfile.tar.gz b/dockerfile.tar.gz deleted file mode 100755 index 7d13010..0000000 Binary files a/dockerfile.tar.gz and /dev/null differ diff --git a/functions.py b/src/functions.py similarity index 60% rename from functions.py rename to src/functions.py index 79760b3..9a8b734 100755 --- a/functions.py +++ b/src/functions.py @@ -1,10 +1,13 @@ -import pdfplumber -import re +"""Function to extract text from pdf and convert it to anki anntation""" +import re import codecs +import pdfplumber + def convert(file_path=False): + """Opens pdf and converts it into text""" if not file_path: import tkinter as tk from tkinter import filedialog @@ -21,23 +24,9 @@ def convert(file_path=False): with pdfplumber.open(file_path) as pdf: for page in pdf.pages: crop = page.crop((60, 80, page.width, page.height)) - # first_page = pdf.pages[0] - # first_page = first_page.crop((60, 80, first_page.width, first_page.height)) text = crop.extract_text(layout=True) no_trail = re.sub("\ +\\n", "\n", text) # cleared trailing spaces - no_wrong_nl = re.sub( - "\\n\ +([A-Za-z0-9])", r" \1", no_trail - ) # clear wrong newlins - lines = re.split("\n", no_wrong_nl) # split into lines - - test = 1 - changed_lines = [] - for line in lines: - line, num = re.subn("(:)(.+)", rf"\1 {{{{c{test}::\2}}}}", line) - if num > 0: - test += 1 - changed_lines.append(line) - new_str = "\n".join(changed_lines).strip() + new_str = convert_text(no_trail) conv_string.append(new_str) conv_string = "#################### neue Seite ####################\n".join( @@ -50,27 +39,28 @@ def convert(file_path=False): text_file.close() print(f"Alles fertig, die Datei befindet sich unter {file_path}") - if not __name__ == "__main__": + if __name__ != "__main__": return conv_string def convert_text(text): + """Seraches for ':' and converts into anki annotation""" text = str(text) if "\r\n" in text: text = text.replace("\r\n", "\n") - no_wrong_nl = re.sub("\\n\ +([A-Za-z0-9])", r" \1", text) # clear wrong newlins + no_wrong_nl = re.sub("\\n\ +([A-Za-z0-9])",r" \1", text) # clear wrong newlins lines = re.split("\n", no_wrong_nl) # split into lines test = 1 changed_lines = [] for line in lines: - line, num = re.subn("(:)(.+)", rf"\1 {{{{c{test}::\2}}}}", line) + line, num = re.subn("(:)(..+)", rf"\1 {{{{c{test}::\2}}}}", line) if num > 0: test += 1 changed_lines.append(line) new_str = "\n".join(changed_lines).strip() - if not __name__ == "__main__": + if __name__ != "__main__": return new_str diff --git a/app.py b/src/main.py similarity index 61% rename from app.py rename to src/main.py index f22bc31..960cff9 100755 --- a/app.py +++ b/src/main.py @@ -1,5 +1,8 @@ -from flask import Flask, render_template, request, redirect, url_for, make_response -from os import makedirs, path +"""Main App Module""" + +from os import makedirs, path, environ + +from flask import Flask, render_template, request import functions app = Flask(__name__) @@ -7,11 +10,13 @@ app = Flask(__name__) @app.route("/") def index(): + """Renders Index Page""" return render_template("index.html") @app.route("/upload", methods=["POST"]) def upload_file(): + """Handles Upload -> Files will be extracted and converted, text just converted""" uploaded_file = request.files["file"] text = request.form["text"] makedirs("uploads", exist_ok=True) @@ -20,6 +25,7 @@ def upload_file(): filepath = path.join("uploads", uploaded_file.filename) uploaded_file.save(filepath) response_text = functions.convert(filepath) + text = response_text # response = make_response(response_text, 200) # response.mimetype = "text/plain" else: @@ -27,10 +33,11 @@ def upload_file(): # response = make_response(response_text, 200) # response.mimetype = "text/plain" # return response #redirect(url_for('index')) - return render_template("index.html", text=response_text) + return render_template("index.html", resp_text=response_text, base_text=text) if __name__ == "__main__": - app.jinja_env.auto_reload = True - app.config["TEMPLATES_AUTO_RELOAD"] = True - app.run(host="0.0.0.0", port=8000) + # app.jinja_env.auto_reload = True + # app.config["TEMPLATES_AUTO_RELOAD"] = True + port = int(environ.get('PORT', 5000)) + app.run(debug=True, host='0.0.0.0', port=port) diff --git a/static/script.js b/src/static/script.js similarity index 100% rename from static/script.js rename to src/static/script.js diff --git a/src/static/style.css b/src/static/style.css new file mode 100755 index 0000000..13fe61e --- /dev/null +++ b/src/static/style.css @@ -0,0 +1,112 @@ +:root{ + --green:#4CAF50; + --primary:#3b3b3b; +} + +*{ + font-family: sans-serif; + font-size: 18px; +} + +body{ + margin: 0; + padding: 0; + color: var(--primary); +} + +main{ + display: grid; + width: 90vw; + margin: 5vw; + flex-wrap: wrap; + grid-template-areas: "head-input head-output" + "input output"; + grid-template-columns: 1fr 1fr; + gap: 1rem; +} + +h1.input{ + grid-area: head-input; +} + +h1.output{ + grid-area: head-output; +} + +h1{ + font-size: 2rem; + margin: 0; + margin-bottom: .5rem; +} + +textarea{ + grid-area: input; +} + +pre{ + grid-area: output; +} + + +main > div{ + width: 100%; + box-sizing: border-box; +} + +textarea{ + width: 100%; + height: 20vh; + margin: 20px 0; + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; +} + +input[type="file"] { + display: none; +} +.custom-file-upload, button { + background-color: white; + color: var(--primary); + border: 2px solid var(--green); /* Green */ + /* background-color: lightgray; */ + display: inline-block; + padding: 6px 12px; + cursor: pointer; + font-size: 1rem; + border-radius: .2rem; + transition-duration: 0.4s; +} + +.custom-file-upload:hover, button:hover { + background-color: var(--green); /* Green */ + color: white; + } + +.custom-file-upload span, button span { + vertical-align: bottom; +} + +.material-symbols-outlined { + padding-right: 0.3rem; +} + +textarea, pre{ + background-color: white; + color: var(--primary); + border: 2px solid var(--green); /* Green */ + border-radius: .2rem; + margin: .5rem 0; + min-height: 10rem; + padding: .2rem; +} + +@media (max-width: 600px) { + main{ + width: 100wv; + } + + main > div{ + width: 100%; + } +} diff --git a/templates/index.html b/src/templates/index.html similarity index 73% rename from templates/index.html rename to src/templates/index.html index fb7eaa9..e7b1de1 100755 --- a/templates/index.html +++ b/src/templates/index.html @@ -1,13 +1,13 @@
-{{ text }}
+ {{ resp_text }}