-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathapp.py
70 lines (54 loc) · 1.87 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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, txt2questions
# 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
questions = dict()
# 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)
questions = txt2questions(uploaded_content)
# 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,
questions=questions,
size=len(questions))
@app.route('/result', methods=['POST', 'GET'])
def result():
correct_q = 0
for k, v in request.form.items():
correct_q += 1
return render_template('result.html', total=5, correct=correct_q)
if __name__ == "__main__":
app.run(debug=True)