Fausto Busuito
commited on
Commit
·
83297c3
1
Parent(s):
3a7f5ab
Application changes
Browse files- app.py +19 -9
- templates/index.html +0 -3
- templates/quiz.html +24 -1
- templates/results.html +1 -1
app.py
CHANGED
@@ -3,6 +3,7 @@ from flask_session import Session
|
|
3 |
import json
|
4 |
import random
|
5 |
import os
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
app.secret_key = 'supersecretkey'
|
@@ -20,11 +21,11 @@ def index():
|
|
20 |
|
21 |
@app.route('/start', methods=['POST'])
|
22 |
def start():
|
23 |
-
session['name'] = request.form['name']
|
24 |
session['questions'] = []
|
25 |
session['answers'] = []
|
26 |
session['score'] = 0
|
27 |
session['current_question'] = 0
|
|
|
28 |
|
29 |
selected_file = request.form['file']
|
30 |
session['selected_file'] = os.path.splitext(selected_file)[0] # Remove file extension
|
@@ -43,10 +44,11 @@ def quiz():
|
|
43 |
if request.method == 'POST':
|
44 |
action = request.form.get('action')
|
45 |
if action == 'next':
|
46 |
-
|
47 |
-
if
|
48 |
-
session['answers'].append(
|
49 |
-
|
|
|
50 |
session['score'] += 1
|
51 |
session['current_question'] += 1
|
52 |
if session['current_question'] >= len(session['questions']):
|
@@ -58,11 +60,17 @@ def quiz():
|
|
58 |
elif action == 'end':
|
59 |
return redirect(url_for('results'))
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
question_number=session['current_question'] + 1,
|
63 |
total_questions=len(session['questions']),
|
64 |
selected_file=session['selected_file'],
|
65 |
-
show_previous=session['current_question'] > 0
|
|
|
|
|
66 |
|
67 |
@app.route('/results')
|
68 |
def results():
|
@@ -71,9 +79,11 @@ def results():
|
|
71 |
|
72 |
total_questions = len(session['questions'])
|
73 |
score_percentage = (session['score'] / total_questions) * 100
|
|
|
74 |
|
75 |
-
return render_template('results.html',
|
76 |
-
total_questions=total_questions, score_percentage=score_percentage
|
|
|
77 |
|
78 |
if __name__ == '__main__':
|
79 |
app.run(host='0.0.0.0', port=7860)
|
|
|
3 |
import json
|
4 |
import random
|
5 |
import os
|
6 |
+
import datetime
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
app.secret_key = 'supersecretkey'
|
|
|
21 |
|
22 |
@app.route('/start', methods=['POST'])
|
23 |
def start():
|
|
|
24 |
session['questions'] = []
|
25 |
session['answers'] = []
|
26 |
session['score'] = 0
|
27 |
session['current_question'] = 0
|
28 |
+
session['start_time'] = datetime.datetime.now()
|
29 |
|
30 |
selected_file = request.form['file']
|
31 |
session['selected_file'] = os.path.splitext(selected_file)[0] # Remove file extension
|
|
|
44 |
if request.method == 'POST':
|
45 |
action = request.form.get('action')
|
46 |
if action == 'next':
|
47 |
+
answers = request.form.getlist('answer')
|
48 |
+
if answers:
|
49 |
+
session['answers'].append(answers)
|
50 |
+
correct_answers = session['questions'][session['current_question']]['correct']
|
51 |
+
if set(answers) == set(correct_answers):
|
52 |
session['score'] += 1
|
53 |
session['current_question'] += 1
|
54 |
if session['current_question'] >= len(session['questions']):
|
|
|
60 |
elif action == 'end':
|
61 |
return redirect(url_for('results'))
|
62 |
|
63 |
+
question = session['questions'][session['current_question']]
|
64 |
+
question_text = question['question']
|
65 |
+
select_multiple = 'SELECT TWO' in question_text or 'SELECT THREE' in question_text
|
66 |
+
|
67 |
+
return render_template('quiz.html', question=question,
|
68 |
question_number=session['current_question'] + 1,
|
69 |
total_questions=len(session['questions']),
|
70 |
selected_file=session['selected_file'],
|
71 |
+
show_previous=session['current_question'] > 0,
|
72 |
+
select_multiple=select_multiple,
|
73 |
+
start_time=session['start_time'])
|
74 |
|
75 |
@app.route('/results')
|
76 |
def results():
|
|
|
79 |
|
80 |
total_questions = len(session['questions'])
|
81 |
score_percentage = (session['score'] / total_questions) * 100
|
82 |
+
elapsed_time = datetime.datetime.now() - session['start_time']
|
83 |
|
84 |
+
return render_template('results.html', score=session['score'],
|
85 |
+
total_questions=total_questions, score_percentage=score_percentage,
|
86 |
+
elapsed_time=str(elapsed_time))
|
87 |
|
88 |
if __name__ == '__main__':
|
89 |
app.run(host='0.0.0.0', port=7860)
|
templates/index.html
CHANGED
@@ -9,9 +9,6 @@
|
|
9 |
<body>
|
10 |
<h1>Welcome to the Quiz App</h1>
|
11 |
<form action="{{ url_for('start') }}" method="post">
|
12 |
-
<label for="name">Enter your name:</label>
|
13 |
-
<input type="text" id="name" name="name" required>
|
14 |
-
<br>
|
15 |
<label for="file">Select a JSON file:</label>
|
16 |
<select id="file" name="file" required>
|
17 |
{% for file in files %}
|
|
|
9 |
<body>
|
10 |
<h1>Welcome to the Quiz App</h1>
|
11 |
<form action="{{ url_for('start') }}" method="post">
|
|
|
|
|
|
|
12 |
<label for="file">Select a JSON file:</label>
|
13 |
<select id="file" name="file" required>
|
14 |
{% for file in files %}
|
templates/quiz.html
CHANGED
@@ -11,7 +11,7 @@
|
|
11 |
<p>{{ question.question }}</p>
|
12 |
<form action="{{ url_for('quiz') }}" method="post">
|
13 |
{% for option in question.options %}
|
14 |
-
<input type="
|
15 |
<label for="{{ loop.index }}">{{ option }}</label><br>
|
16 |
{% endfor %}
|
17 |
<br>
|
@@ -21,5 +21,28 @@
|
|
21 |
<button type="submit" name="action" value="next">Next</button>
|
22 |
<button type="submit" name="action" value="end">End Session</button>
|
23 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
</body>
|
25 |
</html>
|
|
|
11 |
<p>{{ question.question }}</p>
|
12 |
<form action="{{ url_for('quiz') }}" method="post">
|
13 |
{% for option in question.options %}
|
14 |
+
<input type="checkbox" id="{{ loop.index }}" name="answer" value="{{ loop.index }}" {% if not select_multiple %}required{% endif %}>
|
15 |
<label for="{{ loop.index }}">{{ option }}</label><br>
|
16 |
{% endfor %}
|
17 |
<br>
|
|
|
21 |
<button type="submit" name="action" value="next">Next</button>
|
22 |
<button type="submit" name="action" value="end">End Session</button>
|
23 |
</form>
|
24 |
+
<div>
|
25 |
+
<p>Timer: <span id="timer">00:00:00</span></p>
|
26 |
+
<p>System Time: <span id="system-time">{{ start_time.strftime('%H:%M:%S') }}</span></p>
|
27 |
+
</div>
|
28 |
+
<script>
|
29 |
+
function updateTimer() {
|
30 |
+
const startTime = new Date("{{ start_time.isoformat() }}");
|
31 |
+
const now = new Date();
|
32 |
+
const diff = now - startTime;
|
33 |
+
const hours = Math.floor(diff / 3600000);
|
34 |
+
const minutes = Math.floor((diff % 3600000) / 60000);
|
35 |
+
const seconds = Math.floor((diff % 60000) / 1000);
|
36 |
+
document.getElementById('timer').innerText = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
|
37 |
+
}
|
38 |
+
|
39 |
+
function updateSystemTime() {
|
40 |
+
const now = new Date();
|
41 |
+
document.getElementById('system-time').innerText = now.toLocaleTimeString();
|
42 |
+
}
|
43 |
+
|
44 |
+
setInterval(updateTimer, 1000);
|
45 |
+
setInterval(updateSystemTime, 1000);
|
46 |
+
</script>
|
47 |
</body>
|
48 |
</html>
|
templates/results.html
CHANGED
@@ -8,9 +8,9 @@
|
|
8 |
</head>
|
9 |
<body>
|
10 |
<h1>Results</h1>
|
11 |
-
<p>Hello, {{ name }}!</p>
|
12 |
<p>Your score is {{ score }} out of {{ total_questions }}.</p>
|
13 |
<p>That's {{ score_percentage }}%.</p>
|
|
|
14 |
<a href="{{ url_for('index') }}">Take another quiz</a>
|
15 |
</body>
|
16 |
</html>
|
|
|
8 |
</head>
|
9 |
<body>
|
10 |
<h1>Results</h1>
|
|
|
11 |
<p>Your score is {{ score }} out of {{ total_questions }}.</p>
|
12 |
<p>That's {{ score_percentage }}%.</p>
|
13 |
+
<p>Time taken: {{ elapsed_time }}</p>
|
14 |
<a href="{{ url_for('index') }}">Take another quiz</a>
|
15 |
</body>
|
16 |
</html>
|