diff --git a/converter.py b/converter.py index 4c8f146..0a92c48 100644 --- a/converter.py +++ b/converter.py @@ -84,7 +84,8 @@ def write_q_and_a_to_markdown_file(markdown_file, q_and_a): alpha = alphabet[i] markdown_file.writelines(' - {}) {}'.format(alpha, q_and_a.answerArray[i])) - markdown_file.writelines('---') + markdown_file.writelines('\n') + markdown_file.writelines('---') # TODO leave out for the last page? return markdown_file diff --git a/requirements.txt b/requirements.txt index 270c1da..3d442f0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ requests==2.25.1 urllib3==1.26.5 websocket-client==1.1.0 six==1.16.0 # needed for docker to run properly +Flask-Cors==3.0.10 diff --git a/rest_server.py b/rest_server.py new file mode 100644 index 0000000..fe72ec0 --- /dev/null +++ b/rest_server.py @@ -0,0 +1,138 @@ +import os + +from flask import Flask +from flask import render_template +from flask import request, send_from_directory +from flask import url_for +from flask_cors import CORS +# from flask_httpauth import HTTPBasicAuth +# from werkzeug.security import check_password_hash +# import main +from werkzeug.utils import redirect + +# import config_parser +# from md_tagebuch import diary +import converter + +app = Flask(__name__) +# auth = HTTPBasicAuth() +app.config["DEBUG"] = True +CORS(app) + +URL_BASE_PATH = "/" +# URL_BASE_PATH = "/tagebuch" + +# @auth.verify_password +# def verify_password(username, password): +# if username == config_parser.credentials['username'] and \ +# check_password_hash(config_parser.credentials['hashedPassword'], password): +# return username + +OUTPUT_FODLER = "output" +UPLOAD_FODLER = "upload" + +# @app.route(URL_BASE_PATH + 'home', methods=['GET']) +# # @auth.login_required +# def api_show_form(): +# return render_template("index.html") + + +@app.route(URL_BASE_PATH, methods=["GET", "POST"]) +def upload_image(): + if request.method == "POST": + if request.files: + csv_file = request.files["csv"] + print(csv_file) + + if not valid_csv(csv_file.filename): + # TODO give some feedback to user + return redirect(request.url) + + from werkzeug.utils import secure_filename + filename = secure_filename(csv_file.filename) + output_file_path = os.path.join(UPLOAD_FODLER, filename) + csv_file.save(output_file_path) + + # run converter + converter.convert(output_file_path, True) + + return render_template("return_result.html") + # return redirect(request.url) + + return render_template("upload-file.html") + + +def valid_csv(filename): + + # We only want files with a . in the filename + if not "." in filename: + return False + + # Split the extension from the filename + ext = filename.rsplit(".", 1)[1] + return ext == 'csv' + + + +# +# @app.route(URL_BASE_PATH, methods=['POST', 'GET']) +# # @auth.login_required +# def tagebuch(): +# if request.method == "POST": +# user_input = request.form['yearmonth'] +# return redirect(url_for("return_result", yearmonth=user_input)) +# else: +# return render_template("tagebuch.html") +# +# +# @app.route(URL_BASE_PATH + "") +# # @auth.login_required +# def return_result(yearmonth): +# print(os.getcwd()) +# filename = "slide-deck.md" +# +# print("filename to be served: " + filename) +# data = "test" +# full_file_path = os.path.join(OUTPUT_FODLER, filename) +# +# if os.path.exists(full_file_path): +# with open(full_file_path, 'r', encoding='UTF-8') as open_file: +# data = open_file.read() +# # return data +# # print(data) +# # open_file.close() +# # return f"

{yearmonth}

" +# # return "bla" +# return render_template("tagebuch_output.html", markdown_result=data, markdown_filename=filename) + + +@app.route(URL_BASE_PATH + 'download', methods=['GET']) +# @auth.login_required +def download_file(): + """Download the .md file.""" + # print("sending file with name: " + filename) + current_dir = os.getcwd() + full_dir = os.path.join(current_dir, "output") + print("full_dir: " + full_dir) + return send_from_directory(full_dir, "slide-deck.md", as_attachment=True)\ + + +@app.route(URL_BASE_PATH + 'download-pdf', methods=['GET']) +# @auth.login_required +def download_pdf_file(): + """Download the .pdf file.""" + # print("sending file with name: " + filename) + current_dir = os.getcwd() + full_dir = os.path.join(current_dir, "output") + print("full_dir: " + full_dir) + return send_from_directory(full_dir, "slide-deck.pdf", as_attachment=True) + + +if __name__ == '__main__': + flask_options = dict( + host='0.0.0.0', + debug=True, + port=4003, + threaded=True, + ) + app.run(**flask_options) diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..f17840e --- /dev/null +++ b/templates/base.html @@ -0,0 +1,35 @@ + + + + + + + + + {% block title %} {% endblock %} + + + +
+
+
+

{% block pageHeader %} {% endblock %}

+
+ + +

{% block content %} {% endblock %}

+ + + + + +
+
+
+ + + \ No newline at end of file diff --git a/templates/return_result.html b/templates/return_result.html new file mode 100644 index 0000000..ffafe84 --- /dev/null +++ b/templates/return_result.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block pageHeader %} CSV to marp Markdown Converter {% endblock %} + +{% block content %} +

Result

+ + + +

+ + Download Markdown File + Download PDF File +

+{% endblock %} \ No newline at end of file diff --git a/templates/upload-file.html b/templates/upload-file.html new file mode 100644 index 0000000..cd37744 --- /dev/null +++ b/templates/upload-file.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block pageHeader %} CSV to marp Markdown Converter {% endblock %} + +{% block content %} + + + + + +

Upload a CSV file

+ +
+ +
+ + + + + +
+ +
+ +
+ + + +
+ + + +{% endblock %} \ No newline at end of file