Rooni commited on
Commit
6d095b1
1 Parent(s): a14df07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -24,20 +24,19 @@ data = json.loads(json_data)
24
 
25
  def show_questions(test_name):
26
  questions = data['tests'][test_name]['questions']
27
- return {f"question{i+1}": gr.update(visible=True, label=question['text'], info=question['text']) for i, question in enumerate(questions)}, gr.update(visible=False)
 
 
 
28
 
29
- def check(answers, test_name):
30
- questions = data['tests'][test_name]['questions']
31
  results = []
32
- for answer, question in zip(answers, questions):
33
- if answer == question['correct_answer']:
34
- results.append("✔️")
35
- else:
36
- results.append(f"✖️ ({question['incorrect_text']})")
37
- return "\n".join(results), gr.update(visible=True)
38
 
39
- def reset(questions):
40
- return {f"question{i+1}": gr.update(visible=False) for i in range(len(questions))}, gr.update(visible=True)
41
 
42
  css = """
43
  footer {visibility: hidden !important;}
@@ -47,13 +46,18 @@ with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
47
  with gr.Row():
48
  test_selector = gr.Radio(label="Выберите тест", choices=list(data['tests'].keys()))
49
  select_button = gr.Button("Выбрать")
50
- questions = [gr.Radio(label="", choices=["А", "Б", "В"], visible=False) for _ in range(len(max(data['tests'].values(), key=lambda t: len(t['questions']))['questions']))]
 
 
 
 
 
51
  submit_button = gr.Button("Проверить", visible=False)
52
  back_button = gr.Button("Назад", visible=False)
53
  result_output = gr.Markdown(visible=False)
54
 
55
- select_button.click(show_questions, inputs=[test_selector], outputs=[questions, select_button])
56
- submit_button.click(check, inputs=questions + [test_selector], outputs=[result_output, submit_button])
57
- back_button.click(reset, inputs=[questions], outputs=[questions, test_selector])
58
 
59
  vui.launch()
 
24
 
25
  def show_questions(test_name):
26
  questions = data['tests'][test_name]['questions']
27
+ questions_state = {f"question_{i}": question['text'] for i, question in enumerate(questions)}
28
+ correct_answers = [question['correct_answer'] for question in questions]
29
+ incorrect_texts = [question['incorrect_text'] for question in questions]
30
+ return questions_state, correct_answers, incorrect_texts, "", gr.update(visible=True)
31
 
32
+ def check(answers, correct_answers, incorrect_texts):
 
33
  results = []
34
+ for answer, correct_answer, incorrect_text in zip(answers, correct_answers, incorrect_texts):
35
+ results.append("✔️" if answer == correct_answer else f"✖️ ({incorrect_text})")
36
+ return "\n".join(results)
 
 
 
37
 
38
+ def reset():
39
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
40
 
41
  css = """
42
  footer {visibility: hidden !important;}
 
46
  with gr.Row():
47
  test_selector = gr.Radio(label="Выберите тест", choices=list(data['tests'].keys()))
48
  select_button = gr.Button("Выбрать")
49
+
50
+ with gr.Row():
51
+ questions = [gr.Radio(label="", choices=["А", "Б", "В"], visible=False) for _ in range(max(len(test['questions']) for test in data['tests'].values()))]
52
+ for question in questions:
53
+ question.render()
54
+
55
  submit_button = gr.Button("Проверить", visible=False)
56
  back_button = gr.Button("Назад", visible=False)
57
  result_output = gr.Markdown(visible=False)
58
 
59
+ select_button.click(show_questions, inputs=[test_selector], outputs=questions + [result_output, submit_button, back_button])
60
+ submit_button.click(check, inputs=questions + [result_output, submit_button, back_button], outputs=[result_output])
61
+ back_button.click(reset, inputs=[], outputs=[result_output, submit_button, back_button, test_selector])
62
 
63
  vui.launch()