Fausto Busuito
commited on
Commit
•
50b7a69
1
Parent(s):
b4443a9
Application changes
Browse files- Dockerfile +3 -1
- app.py +54 -3
- static/script.js +0 -0
- static/styles.css +25 -0
- templates/index.html +21 -0
- templates/quiz.html +21 -0
- templates/results.html +16 -0
Dockerfile
CHANGED
@@ -7,4 +7,6 @@ RUN pip install -r requirements.txt
|
|
7 |
|
8 |
COPY . .
|
9 |
|
10 |
-
|
|
|
|
|
|
7 |
|
8 |
COPY . .
|
9 |
|
10 |
+
EXPOSE 7860
|
11 |
+
|
12 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
@@ -1,10 +1,61 @@
|
|
1 |
-
from flask import Flask
|
|
|
|
|
|
|
2 |
|
3 |
app = Flask(__name__)
|
|
|
4 |
|
5 |
@app.route('/')
|
6 |
-
def
|
7 |
-
return '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
if __name__ == '__main__':
|
10 |
app.run(host='0.0.0.0', port=7860)
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, session
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
import os
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
+
app.secret_key = 'supersecretkey'
|
8 |
|
9 |
@app.route('/')
|
10 |
+
def index():
|
11 |
+
return render_template('index.html')
|
12 |
+
|
13 |
+
@app.route('/start', methods=['POST'])
|
14 |
+
def start():
|
15 |
+
session['name'] = request.form['name']
|
16 |
+
session['questions'] = []
|
17 |
+
session['answers'] = []
|
18 |
+
session['score'] = 0
|
19 |
+
session['current_question'] = 0
|
20 |
+
|
21 |
+
file = request.files['file']
|
22 |
+
if file:
|
23 |
+
questions = json.load(file)
|
24 |
+
random.shuffle(questions)
|
25 |
+
session['questions'] = questions
|
26 |
+
return redirect(url_for('quiz'))
|
27 |
+
return redirect(url_for('index'))
|
28 |
+
|
29 |
+
@app.route('/quiz', methods=['GET', 'POST'])
|
30 |
+
def quiz():
|
31 |
+
if 'questions' not in session or 'current_question' not in session:
|
32 |
+
return redirect(url_for('index'))
|
33 |
+
|
34 |
+
if request.method == 'POST':
|
35 |
+
answer = request.form.get('answer')
|
36 |
+
if answer:
|
37 |
+
session['answers'].append(answer)
|
38 |
+
if answer in session['questions'][session['current_question']]['correct']:
|
39 |
+
session['score'] += 1
|
40 |
+
|
41 |
+
session['current_question'] += 1
|
42 |
+
if session['current_question'] >= len(session['questions']):
|
43 |
+
return redirect(url_for('results'))
|
44 |
+
|
45 |
+
return render_template('quiz.html', question=session['questions'][session['current_question']],
|
46 |
+
question_number=session['current_question'] + 1,
|
47 |
+
total_questions=len(session['questions']))
|
48 |
+
|
49 |
+
@app.route('/results')
|
50 |
+
def results():
|
51 |
+
if 'questions' not in session or 'answers' not in session:
|
52 |
+
return redirect(url_for('index'))
|
53 |
+
|
54 |
+
total_questions = len(session['questions'])
|
55 |
+
score_percentage = (session['score'] / total_questions) * 100
|
56 |
+
|
57 |
+
return render_template('results.html', name=session['name'], score=session['score'],
|
58 |
+
total_questions=total_questions, score_percentage=score_percentage)
|
59 |
|
60 |
if __name__ == '__main__':
|
61 |
app.run(host='0.0.0.0', port=7860)
|
static/script.js
ADDED
File without changes
|
static/styles.css
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
font-family: Arial, sans-serif;
|
3 |
+
margin: 20px;
|
4 |
+
}
|
5 |
+
|
6 |
+
h1 {
|
7 |
+
color: #333;
|
8 |
+
}
|
9 |
+
|
10 |
+
form {
|
11 |
+
margin-top: 20px;
|
12 |
+
}
|
13 |
+
|
14 |
+
label {
|
15 |
+
display: block;
|
16 |
+
margin-top: 10px;
|
17 |
+
}
|
18 |
+
|
19 |
+
input[type="text"], input[type="file"] {
|
20 |
+
margin-top: 5px;
|
21 |
+
}
|
22 |
+
|
23 |
+
button {
|
24 |
+
margin-top: 20px;
|
25 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Quiz App</title>
|
7 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<h1>Welcome to the Quiz App</h1>
|
11 |
+
<form action="{{ url_for('start') }}" method="post" enctype="multipart/form-data">
|
12 |
+
<label for="name">Enter your name:</label>
|
13 |
+
<input type="text" id="name" name="name" required>
|
14 |
+
<br>
|
15 |
+
<label for="file">Upload JSON file:</label>
|
16 |
+
<input type="file" id="file" name="file" required>
|
17 |
+
<br>
|
18 |
+
<button type="submit">Start Quiz</button>
|
19 |
+
</form>
|
20 |
+
</body>
|
21 |
+
</html>
|
templates/quiz.html
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Quiz</title>
|
7 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<h1>Question {{ question_number }} of {{ total_questions }}</h1>
|
11 |
+
<p>{{ question.question }}</p>
|
12 |
+
<form action="{{ url_for('quiz') }}" method="post">
|
13 |
+
{% for option in question.options %}
|
14 |
+
<input type="radio" id="{{ loop.index }}" name="answer" value="{{ loop.index }}" required>
|
15 |
+
<label for="{{ loop.index }}">{{ option }}</label><br>
|
16 |
+
{% endfor %}
|
17 |
+
<br>
|
18 |
+
<button type="submit">Next</button>
|
19 |
+
</form>
|
20 |
+
</body>
|
21 |
+
</html>
|
templates/results.html
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Results</title>
|
7 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
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>
|