38894-vm/app.py

53 lines
1.5 KiB
Python

import os
from flask import Flask, render_template, redirect, url_for
from flask.globals import request
from werkzeug.utils import secure_filename
from workers import pdf2text
# Constants
UPLOAD_FOLDER = './pdf/'
# Init an app object
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@ app.route('/')
def index():
""" The landing page for the app """
return render_template('index.html')
@ app.route('/quiz', methods=['GET', 'POST'])
def quiz():
""" Handle upload and conversion of file + other stuff """
UPLOAD_STATUS = False
# Make directory to store uploaded files, if not exists
if not os.path.isdir('./pdf'):
os.mkdir('./pdf')
if request.method == 'POST':
try:
# Retrieve file from request
uploaded_file = request.files['file']
file_path = os.path.join(
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename))
file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower()
# Save uploaded file
uploaded_file.save(file_path)
# Get contents of file
uploaded_content = pdf2text(file_path, file_exten)
# File upload + convert success
if uploaded_content is not None:
UPLOAD_STATUS = True
except Exception as e:
print(e)
return render_template('quiz.html', uploaded=UPLOAD_STATUS)
if __name__ == "__main__":
app.run(debug=True)