Fixed options issue on quiz screen

This commit is contained in:
user86 2020-10-11 11:31:58 +05:30
parent 7d91ff2c49
commit 8e51d930f9
4 changed files with 137 additions and 146 deletions

3
app.py
View File

@ -24,6 +24,7 @@ def quiz():
""" Handle upload and conversion of file + other stuff """ """ Handle upload and conversion of file + other stuff """
UPLOAD_STATUS = False UPLOAD_STATUS = False
questions = dict()
# Make directory to store uploaded files, if not exists # Make directory to store uploaded files, if not exists
if not os.path.isdir('./pdf'): if not os.path.isdir('./pdf'):
@ -36,13 +37,13 @@ def quiz():
file_path = os.path.join( file_path = os.path.join(
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)) app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename))
file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower() file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower()
questions = ''
# Save uploaded file # Save uploaded file
uploaded_file.save(file_path) uploaded_file.save(file_path)
# Get contents of file # Get contents of file
uploaded_content = pdf2text(file_path, file_exten) uploaded_content = pdf2text(file_path, file_exten)
questions = txt2questions(uploaded_content) questions = txt2questions(uploaded_content)
print(questions)
# File upload + convert success # File upload + convert success
if uploaded_content is not None: if uploaded_content is not None:

View File

@ -1,31 +1,21 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" />
rel="stylesheet" <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/style.css') }}" />
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> <script src="https://kit.fontawesome.com/22be60108b.js" crossorigin="anonymous"></script>
<title>MLH Quizzet</title> <title>MLH Quizzet</title>
</head> </head>
<body> <body>
<nav <nav class="navbar is-dark is-fixed-top has-text-centered" role="navigation" aria-label="main navigation">
class="navbar is-dark is-fixed-top has-text-centered" <div class="navbar-brand"><img src="https://avatars0.githubusercontent.com/u/65834464?s=200&amp;v=4" height="32"
role="navigation" width="64" style="margin: 10px">
aria-label="main navigation" <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-brand"><img src="https://avatars0.githubusercontent.com/u/65834464?s=200&amp;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"> <div class="navbar-burger" data-target="navMenu">
<span></span> <span></span>
<span></span> <span></span>
@ -35,23 +25,12 @@
</nav> </nav>
<main role="main"> <main role="main">
<div class="card has-text-centered"> <div class="card has-text-centered">
<img <img src="https://cdn4.iconfinder.com/data/icons/files-and-folders-thinline-icons-set/144/File_PDF-512.png"
src="https://cdn4.iconfinder.com/data/icons/files-and-folders-thinline-icons-set/144/File_PDF-512.png" alt="upload" />
alt="upload" <form action="http://localhost:5000/quiz" method="POST" enctype="multipart/form-data">
/>
<form
action="http://localhost:5000/quiz"
method="POST"
enctype="multipart/form-data"
>
<div id="file-js-example" class="file has-name is-fullwidth"> <div id="file-js-example" class="file has-name is-fullwidth">
<label class="file-label"> <label class="file-label">
<input <input class="file-input" type="file" name="file" accept=".txt, application/pdf" />
class="file-input"
type="file"
name="file"
accept=".txt, application/pdf"
/>
<span class="file-cta"> <span class="file-cta">
<span class="file-icon"> <span class="file-icon">
<i class="fas fa-upload"></i> <i class="fas fa-upload"></i>
@ -76,8 +55,7 @@
}; };
</script> </script>
<button class="button has-text-centered is-large is-fullwidth is-dark"> <button class="button has-text-centered is-large is-fullwidth is-dark">
<span class="submit has-text-centered" type="submit" <span class="submit has-text-centered" type="submit" name="upload file">Submit</span>
name="upload file">Submit</span>
<span class="loading"><i class="fa fa-refresh"></i></span> <span class="loading"><i class="fa fa-refresh"></i></span>
<span class="check"><i class="fa fa-check"></i></span> <span class="check"><i class="fa fa-check"></i></span>
</button> </button>
@ -99,4 +77,5 @@
button.addEventListener('transitionend', toggleClass); button.addEventListener('transitionend', toggleClass);
button.addEventListener('transitionend', addClass);</script> button.addEventListener('transitionend', addClass);</script>
</body> </body>
</html> </html>

View File

@ -22,7 +22,8 @@
<nav class="navbar is-dark is-fixed-top has-text-centered" role="navigation" aria-label="main navigation"> <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&amp;v=4" height="32" <div class="navbar-brand"><img src="https://avatars0.githubusercontent.com/u/65834464?s=200&amp;v=4" height="32"
width="64" style="margin: 10px"> 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> Quizzet</strong></a>
<div class="navbar-burger" data-target="navMenu"> <div class="navbar-burger" data-target="navMenu">
<span></span> <span></span>
@ -40,13 +41,14 @@
<h3>{{ i+1 }}. {{ questions[i+1]['question'] }}</h3> <h3>{{ i+1 }}. {{ questions[i+1]['question'] }}</h3>
<br /> <br />
<form> <form>
{% for j in questions[i+1]['options'] %} {% for op in questions[i+1]['options'] %}
{% if questions[i+1]['options'][j+1] == questions[i+1]['answer'] %} {% if op == questions[i+1]['answer'] %}
<input type="radio" id="{{ questions[i+1]['answer'] }}" name="question{{ i+1 }}"> <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 /> <label for="{{ questions[i+1]['answer'] }}">
{{ op }}</label><br />
{% else %} {% else %}
<input type="radio" name="question{{ i+1 }}"> <input type="radio" name="question{{ i+1 }}">
<label>{{ j }}. {{ questions[i+1]['options'][j+1] }}</label><br /> <label> {{ op }}</label><br />
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>
@ -56,7 +58,9 @@
</section> </section>
{% endfor %} {% endfor %}
{% else %} {% else %}
<section class="section-1" id="section-1">
<h1>Could not upload file</h1> <h1>Could not upload file</h1>
</section>
{% endif %} {% endif %}

View File

@ -2,7 +2,7 @@ from PyPDF2 import PdfFileReader
from question_generation_main import QuestionGeneration 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 """ """ Converts a given file to text content """
_content = '' _content = ''
@ -11,9 +11,9 @@ def pdf2text(file_path, file_exten) -> str:
if file_exten == 'pdf': if file_exten == 'pdf':
with open(file_path, 'rb') as pdf_file: with open(file_path, 'rb') as pdf_file:
_pdf_reader = PdfFileReader(pdf_file) _pdf_reader = PdfFileReader(pdf_file)
# for p in range(_pdf_reader.numPages): for p in range(_pdf_reader.numPages):
# _content += _pdf_reader.getPage(p).extractText() _content += _pdf_reader.getPage(p).extractText()
_content = _pdf_reader.getPage(0).extractText() # _content = _pdf_reader.getPage(0).extractText()
print('PDF operation done!') print('PDF operation done!')
elif file_exten == 'txt': elif file_exten == 'txt':
@ -24,8 +24,15 @@ def pdf2text(file_path, file_exten) -> str:
return _content return _content
def txt2questions(doc, n=1, o=4): def txt2questions(doc: str, n=1, o=4) -> dict:
""" Get all questions and options """ """ Get all questions and options """
qGen = QuestionGeneration(n, o) 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