Fixed options issue on quiz screen
This commit is contained in:
parent
7d91ff2c49
commit
8e51d930f9
3
app.py
3
app.py
@ -24,6 +24,7 @@ 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'):
|
||||
@ -36,13 +37,13 @@ def quiz():
|
||||
file_path = os.path.join(
|
||||
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename))
|
||||
file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower()
|
||||
questions = ''
|
||||
|
||||
# Save uploaded file
|
||||
uploaded_file.save(file_path)
|
||||
# Get contents of file
|
||||
uploaded_content = pdf2text(file_path, file_exten)
|
||||
questions = txt2questions(uploaded_content)
|
||||
print(questions)
|
||||
|
||||
# File upload + convert success
|
||||
if uploaded_content is not None:
|
||||
|
||||
@ -1,102 +1,81 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css"
|
||||
/>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
type="text/css"
|
||||
href="{{ url_for('static',filename='css/style.css') }}"
|
||||
/>
|
||||
<script src="https://kit.fontawesome.com/22be60108b.js" crossorigin="anonymous"></script>
|
||||
<title>MLH Quizzet</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<nav
|
||||
class="navbar is-dark is-fixed-top has-text-centered"
|
||||
role="navigation"
|
||||
aria-label="main navigation"
|
||||
>
|
||||
<div class="navbar-brand"><img src="https://avatars0.githubusercontent.com/u/65834464?s=200&v=4" height="32" width="64" style="margin: 10px">
|
||||
<a class="navbar-item has-text-centered" href="#"
|
||||
><strong class="is-size-3 has-text-centered">MLH Quizzet</strong></a
|
||||
>
|
||||
<div class="navbar-burger" data-target="navMenu">
|
||||
<span></span>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</div>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" />
|
||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/style.css') }}" />
|
||||
<script src="https://kit.fontawesome.com/22be60108b.js" crossorigin="anonymous"></script>
|
||||
<title>MLH Quizzet</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<nav class="navbar is-dark is-fixed-top has-text-centered" role="navigation" aria-label="main navigation">
|
||||
<div class="navbar-brand"><img src="https://avatars0.githubusercontent.com/u/65834464?s=200&v=4" height="32"
|
||||
width="64" style="margin: 10px">
|
||||
<a class="navbar-item has-text-centered" href="{{ url_for('index') }}"><strong
|
||||
class="is-size-3 has-text-centered">MLH Quizzet</strong></a>
|
||||
<div class="navbar-burger" data-target="navMenu">
|
||||
<span></span>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</div>
|
||||
</nav>
|
||||
<main role="main">
|
||||
<div class="card has-text-centered">
|
||||
<img
|
||||
src="https://cdn4.iconfinder.com/data/icons/files-and-folders-thinline-icons-set/144/File_PDF-512.png"
|
||||
alt="upload"
|
||||
/>
|
||||
<form
|
||||
action="http://localhost:5000/quiz"
|
||||
method="POST"
|
||||
enctype="multipart/form-data"
|
||||
>
|
||||
<div id="file-js-example" class="file has-name is-fullwidth">
|
||||
<label class="file-label">
|
||||
<input
|
||||
class="file-input"
|
||||
type="file"
|
||||
name="file"
|
||||
accept=".txt, application/pdf"
|
||||
/>
|
||||
<span class="file-cta">
|
||||
<span class="file-icon">
|
||||
<i class="fas fa-upload"></i>
|
||||
</span>
|
||||
<span class="file-label"> Choose a file… </span>
|
||||
</div>
|
||||
</nav>
|
||||
<main role="main">
|
||||
<div class="card has-text-centered">
|
||||
<img src="https://cdn4.iconfinder.com/data/icons/files-and-folders-thinline-icons-set/144/File_PDF-512.png"
|
||||
alt="upload" />
|
||||
<form action="http://localhost:5000/quiz" method="POST" enctype="multipart/form-data">
|
||||
<div id="file-js-example" class="file has-name is-fullwidth">
|
||||
<label class="file-label">
|
||||
<input class="file-input" type="file" name="file" accept=".txt, application/pdf" />
|
||||
<span class="file-cta">
|
||||
<span class="file-icon">
|
||||
<i class="fas fa-upload"></i>
|
||||
</span>
|
||||
<span class="file-name"> No file uploaded </span>
|
||||
</label>
|
||||
</div>
|
||||
<span class="file-label"> Choose a file… </span>
|
||||
</span>
|
||||
<span class="file-name"> No file uploaded </span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const fileInput = document.querySelector(
|
||||
"#file-js-example input[type=file]"
|
||||
);
|
||||
fileInput.onchange = () => {
|
||||
if (fileInput.files.length > 0) {
|
||||
const fileName = document.querySelector(
|
||||
"#file-js-example .file-name"
|
||||
);
|
||||
fileName.textContent = fileInput.files[0].name;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
<button class="button has-text-centered is-large is-fullwidth is-dark">
|
||||
<span class="submit has-text-centered" type="submit"
|
||||
name="upload file">Submit</span>
|
||||
<span class="loading"><i class="fa fa-refresh"></i></span>
|
||||
<span class="check"><i class="fa fa-check"></i></span>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
<script>const button = document.querySelector('.button');
|
||||
const submit = document.querySelector('.submit');
|
||||
<script>
|
||||
const fileInput = document.querySelector(
|
||||
"#file-js-example input[type=file]"
|
||||
);
|
||||
fileInput.onchange = () => {
|
||||
if (fileInput.files.length > 0) {
|
||||
const fileName = document.querySelector(
|
||||
"#file-js-example .file-name"
|
||||
);
|
||||
fileName.textContent = fileInput.files[0].name;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
<button class="button has-text-centered is-large is-fullwidth is-dark">
|
||||
<span class="submit has-text-centered" type="submit" name="upload file">Submit</span>
|
||||
<span class="loading"><i class="fa fa-refresh"></i></span>
|
||||
<span class="check"><i class="fa fa-check"></i></span>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
<script>const button = document.querySelector('.button');
|
||||
const submit = document.querySelector('.submit');
|
||||
|
||||
function toggleClass() {
|
||||
this.classList.toggle('active');
|
||||
}
|
||||
function toggleClass() {
|
||||
this.classList.toggle('active');
|
||||
}
|
||||
|
||||
function addClass() {
|
||||
this.classList.add('finished');
|
||||
}
|
||||
function addClass() {
|
||||
this.classList.add('finished');
|
||||
}
|
||||
|
||||
button.addEventListener('click', toggleClass);
|
||||
button.addEventListener('transitionend', toggleClass);
|
||||
button.addEventListener('transitionend', addClass);</script>
|
||||
</body>
|
||||
</html>
|
||||
button.addEventListener('click', toggleClass);
|
||||
button.addEventListener('transitionend', toggleClass);
|
||||
button.addEventListener('transitionend', addClass);</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@ -1,28 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" />
|
||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/quiz.css') }}" />
|
||||
<title>MLH Quizzet</title>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type='text/javascript'></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function () {
|
||||
$('label').click(function () {
|
||||
$('label').removeClass('worngans');
|
||||
$(this).addClass('worngans');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" />
|
||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/quiz.css') }}" />
|
||||
<title>MLH Quizzet</title>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type='text/javascript'></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function () {
|
||||
$('label').click(function () {
|
||||
$('label').removeClass('worngans');
|
||||
$(this).addClass('worngans');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<body>
|
||||
<nav class="navbar is-dark is-fixed-top has-text-centered" role="navigation" aria-label="main navigation">
|
||||
<div class="navbar-brand"><img src="https://avatars0.githubusercontent.com/u/65834464?s=200&v=4" height="32"
|
||||
width="64" style="margin: 10px">
|
||||
<a class="navbar-item has-text-centered" href="#"><strong class="is-size-3 has-text-centered">MLH
|
||||
<a class="navbar-item has-text-centered" href="{{ url_for('index') }}"><strong
|
||||
class="is-size-3 has-text-centered">MLH
|
||||
Quizzet</strong></a>
|
||||
<div class="navbar-burger" data-target="navMenu">
|
||||
<span></span>
|
||||
@ -31,35 +32,38 @@
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
{% if uploaded == true %}
|
||||
{% for i in range(size) %}
|
||||
<section class="section-1" id="section-1">
|
||||
<main>
|
||||
<div class="scp-quizzes-main">
|
||||
<div class="scp-quizzes-data">
|
||||
<h3>{{ i+1 }}. {{ questions[i+1]['question'] }}</h3>
|
||||
<br />
|
||||
<form>
|
||||
{% for j in questions[i+1]['options'] %}
|
||||
{% if questions[i+1]['options'][j+1] == questions[i+1]['answer'] %}
|
||||
<input type="radio" id="{{ questions[i+1]['answer'] }}" name="question{{ i+1 }}">
|
||||
<label for="{{ questions[i+1]['answer'] }}">{{ j }}. {{ questions[i+1]['options'][j+1] }}</label><br />
|
||||
{% else %}
|
||||
<input type="radio" name="question{{ i+1 }}">
|
||||
<label>{{ j }}. {{ questions[i+1]['options'][j+1] }}</label><br />
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
{% if uploaded == true %}
|
||||
{% for i in range(size) %}
|
||||
<section class="section-1" id="section-1">
|
||||
<main>
|
||||
<div class="scp-quizzes-main">
|
||||
<div class="scp-quizzes-data">
|
||||
<h3>{{ i+1 }}. {{ questions[i+1]['question'] }}</h3>
|
||||
<br />
|
||||
<form>
|
||||
{% for op in questions[i+1]['options'] %}
|
||||
{% if op == questions[i+1]['answer'] %}
|
||||
<input type="radio" id="{{ questions[i+1]['answer'] }}" name="question{{ i+1 }}">
|
||||
<label for="{{ questions[i+1]['answer'] }}">
|
||||
{{ op }}</label><br />
|
||||
{% else %}
|
||||
<input type="radio" name="question{{ i+1 }}">
|
||||
<label> {{ op }}</label><br />
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
</section>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<h1>Could not upload file</h1>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<section class="section-1" id="section-1">
|
||||
<h1>Could not upload file</h1>
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
|
||||
</body>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
19
workers.py
19
workers.py
@ -2,7 +2,7 @@ from PyPDF2 import PdfFileReader
|
||||
from question_generation_main import QuestionGeneration
|
||||
|
||||
|
||||
def pdf2text(file_path, file_exten) -> str:
|
||||
def pdf2text(file_path: str, file_exten: str) -> str:
|
||||
""" Converts a given file to text content """
|
||||
|
||||
_content = ''
|
||||
@ -11,9 +11,9 @@ def pdf2text(file_path, file_exten) -> str:
|
||||
if file_exten == 'pdf':
|
||||
with open(file_path, 'rb') as pdf_file:
|
||||
_pdf_reader = PdfFileReader(pdf_file)
|
||||
# for p in range(_pdf_reader.numPages):
|
||||
# _content += _pdf_reader.getPage(p).extractText()
|
||||
_content = _pdf_reader.getPage(0).extractText()
|
||||
for p in range(_pdf_reader.numPages):
|
||||
_content += _pdf_reader.getPage(p).extractText()
|
||||
# _content = _pdf_reader.getPage(0).extractText()
|
||||
print('PDF operation done!')
|
||||
|
||||
elif file_exten == 'txt':
|
||||
@ -24,8 +24,15 @@ def pdf2text(file_path, file_exten) -> str:
|
||||
return _content
|
||||
|
||||
|
||||
def txt2questions(doc, n=1, o=4):
|
||||
def txt2questions(doc: str, n=1, o=4) -> dict:
|
||||
""" Get all questions and options """
|
||||
|
||||
qGen = QuestionGeneration(n, o)
|
||||
return qGen.generate_questions_dict(doc)
|
||||
q = qGen.generate_questions_dict(doc)
|
||||
for i in range(len(q)):
|
||||
temp = []
|
||||
for j in range(len(q[i+1]['options'])):
|
||||
temp.append(q[i+1]['options'][j+1])
|
||||
# print(temp)
|
||||
q[i+1]['options'] = temp
|
||||
return q
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user