Fausto Busuito commited on
Commit
0d1bedf
1 Parent(s): 50b7a69

Application changes

Browse files
Files changed (2) hide show
  1. app.py +8 -5
  2. templates/index.html +7 -3
app.py CHANGED
@@ -6,9 +6,12 @@ import os
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():
@@ -18,13 +21,13 @@ def start():
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():
 
6
  app = Flask(__name__)
7
  app.secret_key = 'supersecretkey'
8
 
9
+ QUESTIONS_FOLDER = 'questions'
10
+
11
  @app.route('/')
12
  def index():
13
+ files = [f for f in os.listdir(QUESTIONS_FOLDER) if f.endswith('.json')]
14
+ return render_template('index.html', files=files)
15
 
16
  @app.route('/start', methods=['POST'])
17
  def start():
 
21
  session['score'] = 0
22
  session['current_question'] = 0
23
 
24
+ selected_file = request.form['file']
25
+ file_path = os.path.join(QUESTIONS_FOLDER, selected_file)
26
+ with open(file_path, 'r') as file:
27
  questions = json.load(file)
28
  random.shuffle(questions)
29
  session['questions'] = questions
30
+ return redirect(url_for('quiz'))
 
31
 
32
  @app.route('/quiz', methods=['GET', 'POST'])
33
  def quiz():
templates/index.html CHANGED
@@ -8,12 +8,16 @@
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>
 
8
  </head>
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 %}
18
+ <option value="{{ file }}">{{ file }}</option>
19
+ {% endfor %}
20
+ </select>
21
  <br>
22
  <button type="submit">Start Quiz</button>
23
  </form>