johnpaulbin commited on
Commit
8267305
1 Parent(s): 513dc9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -66,23 +66,27 @@ def display_mock_question(index, score, incorrect_questions):
66
  return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
67
 
68
 
69
- def handle_answer(index, answer, score, incorrect_questions):
70
  data = load_mocktest()
71
- correct_answer = data[index]["correct-answer"]
72
 
73
  if answer == correct_answer:
74
  score += 1
75
  else:
76
- incorrect_questions.append(data[index]["question"])
77
 
78
- index += 1
79
- if index >= len(data):
80
  incorrect_str = "Questions to review:\n\n" + "\n\n".join(incorrect_questions) if incorrect_questions else "No questions to review."
81
  result = f"Your score: {score}/{len(data)}\n\n{incorrect_str}"
82
- return gr.update(visible=False), result, gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), score, incorrect_questions
83
 
84
- question, ans1, ans2, ans3, ans4, correct = display_mock_question(index, score, incorrect_questions)
85
- return gr.update(value=question), ans1, ans2, ans3, ans4, gr.update(visible=False), gr.update(visible=False), score, incorrect_questions
 
 
 
 
86
 
87
  # Function to restart the test
88
  def restart_test():
@@ -136,17 +140,21 @@ with gr.Blocks() as demo:
136
  restart_button = gr.Button("Restart Test", visible=False)
137
 
138
  # Initialize the first question
139
- demo.load(display_mock_question, inputs=[mock_index, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4])
140
 
141
  # Handle answer selection
142
  for answer_btn in [answer_1, answer_2, answer_3, answer_4]:
143
  answer_btn.click(
144
  handle_answer,
145
- inputs=[mock_index, answer_btn, score, incorrect_questions],
146
- outputs=[question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button, score, incorrect_questions]
 
 
 
 
147
  )
148
 
149
  # Restart test button
150
  restart_button.click(restart_test, inputs=[], outputs=[mock_index, score, incorrect_questions, question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button])
151
- # Launch the app
152
  demo.launch()
 
66
  return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
67
 
68
 
69
+ def handle_answer(answer, mock_index, score, incorrect_questions):
70
  data = load_mocktest()
71
+ correct_answer = data[mock_index]["correct-answer"]
72
 
73
  if answer == correct_answer:
74
  score += 1
75
  else:
76
+ incorrect_questions.append(data[mock_index]["question"])
77
 
78
+ mock_index += 1
79
+ if mock_index >= len(data):
80
  incorrect_str = "Questions to review:\n\n" + "\n\n".join(incorrect_questions) if incorrect_questions else "No questions to review."
81
  result = f"Your score: {score}/{len(data)}\n\n{incorrect_str}"
82
+ return gr.update(value=result), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), mock_index, score, incorrect_questions
83
 
84
+ question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score, incorrect_questions)
85
+ return gr.update(value=question), gr.update(visible=False), gr.update(value=ans1), gr.update(value=ans2), gr.update(value=ans3), gr.update(value=ans4), mock_index, score, incorrect_questions
86
+
87
+ def update_question(mock_index, score, incorrect_questions):
88
+ question, ans1, ans2, ans3, ans4, correct = display_mock_question(mock_index, score, incorrect_questions)
89
+ return gr.update(value=question), gr.update(value=ans1), gr.update(value=ans2), gr.update(value=ans3), gr.update(value=ans4)
90
 
91
  # Function to restart the test
92
  def restart_test():
 
140
  restart_button = gr.Button("Restart Test", visible=False)
141
 
142
  # Initialize the first question
143
+ demo.load(update_question, inputs=[mock_index, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4])
144
 
145
  # Handle answer selection
146
  for answer_btn in [answer_1, answer_2, answer_3, answer_4]:
147
  answer_btn.click(
148
  handle_answer,
149
+ inputs=[answer_btn, mock_index, score, incorrect_questions],
150
+ outputs=[question_output, restart_button, answer_1, answer_2, answer_3, answer_4, mock_index, score, incorrect_questions]
151
+ ).then(
152
+ update_question,
153
+ inputs=[mock_index, score, incorrect_questions],
154
+ outputs=[question_output, answer_1, answer_2, answer_3, answer_4]
155
  )
156
 
157
  # Restart test button
158
  restart_button.click(restart_test, inputs=[], outputs=[mock_index, score, incorrect_questions, question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button])
159
+
160
  demo.launch()