Update app.py
Browse files
app.py
CHANGED
@@ -24,19 +24,25 @@ data = json.loads(json_data)
|
|
24 |
|
25 |
def show_questions(test_name):
|
26 |
questions = data['tests'][test_name]['questions']
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
|
32 |
def check(answers, correct_answers, incorrect_texts):
|
33 |
results = []
|
34 |
-
for answer,
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
def reset():
|
39 |
-
return gr.update(visible=False
|
|
|
|
|
|
|
40 |
|
41 |
css = """
|
42 |
footer {visibility: hidden !important;}
|
@@ -48,16 +54,15 @@ with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
|
48 |
select_button = gr.Button("Выбрать")
|
49 |
|
50 |
with gr.Row():
|
51 |
-
|
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=
|
60 |
-
submit_button.click(check, inputs=
|
61 |
-
back_button.click(reset, inputs=[], outputs=[result_output, submit_button,
|
62 |
|
63 |
vui.launch()
|
|
|
24 |
|
25 |
def show_questions(test_name):
|
26 |
questions = data['tests'][test_name]['questions']
|
27 |
+
return [gr.update(visible=True, label=q['text'], value='') for q in questions] + \
|
28 |
+
[gr.update(visible=False)] * (max_questions - len(questions)), \
|
29 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), \
|
30 |
+
[q['correct_answer'] for q in questions], [q['incorrect_text'] for q in questions]
|
31 |
|
32 |
def check(answers, correct_answers, incorrect_texts):
|
33 |
results = []
|
34 |
+
for answer, correct, incorrect in zip(answers, correct_answers, incorrect_texts):
|
35 |
+
if answer == correct:
|
36 |
+
results.append("✔️")
|
37 |
+
else:
|
38 |
+
results.append(f"✖️ {incorrect}")
|
39 |
+
return "\n".join(results), gr.update(visible=True)
|
40 |
|
41 |
def reset():
|
42 |
+
return [gr.update(visible=False, value='') for _ in range(max_questions)], \
|
43 |
+
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
44 |
+
|
45 |
+
max_questions = max(len(test['questions']) for test in data['tests'].values())
|
46 |
|
47 |
css = """
|
48 |
footer {visibility: hidden !important;}
|
|
|
54 |
select_button = gr.Button("Выбрать")
|
55 |
|
56 |
with gr.Row():
|
57 |
+
question_blocks = [gr.Radio(choices=["А", "Б", "В"], visible=False) for _ in range(max_questions)]
|
|
|
|
|
|
|
58 |
submit_button = gr.Button("Проверить", visible=False)
|
59 |
back_button = gr.Button("Назад", visible=False)
|
60 |
result_output = gr.Markdown(visible=False)
|
61 |
+
correct_answers_output = gr.Hidden()
|
62 |
+
incorrect_texts_output = gr.Hidden()
|
63 |
|
64 |
+
select_button.click(show_questions, inputs=[test_selector], outputs=question_blocks + [select_button, submit_button, back_button, correct_answers_output, incorrect_texts_output])
|
65 |
+
submit_button.click(check, inputs=question_blocks + [correct_answers_output, incorrect_texts_output], outputs=[result_output, submit_button])
|
66 |
+
back_button.click(reset, inputs=[], outputs=question_blocks + [result_output, submit_button, test_selector, back_button])
|
67 |
|
68 |
vui.launch()
|