Update app.py
Browse files
app.py
CHANGED
|
@@ -22,27 +22,28 @@ json_data = """
|
|
| 22 |
|
| 23 |
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, 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
max_questions = max(len(test['questions']) for test in data['tests'].values())
|
| 46 |
|
| 47 |
css = """
|
| 48 |
footer {visibility: hidden !important;}
|
|
@@ -58,11 +59,20 @@ with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
vui.launch()
|
|
|
|
| 22 |
|
| 23 |
data = json.loads(json_data)
|
| 24 |
|
| 25 |
+
# Получаем максимальное количество вопросов в тестах для создания радио кнопок
|
| 26 |
+
max_questions = max(len(test['questions']) for test in data['tests'].values())
|
| 27 |
+
|
| 28 |
def show_questions(test_name):
|
| 29 |
questions = data['tests'][test_name]['questions']
|
| 30 |
+
correct_answers = [q['correct_answer'] for q in questions]
|
| 31 |
+
incorrect_texts = [q['incorrect_text'] for q in questions]
|
| 32 |
+
# Возвращаем состояния видимости и текст вопросов для радио кнопок
|
| 33 |
+
outputs = [gr.update(visible=True, label=q['text']) for q in questions] + \
|
| 34 |
+
[gr.update(visible=False)] * (max_questions - len(questions))
|
| 35 |
+
# Возвращаем состояния видимости для кнопок и результатов
|
| 36 |
+
return outputs, gr.update(visible=True), gr.update(visible=False), correct_answers, incorrect_texts
|
| 37 |
|
| 38 |
def check(answers, correct_answers, incorrect_texts):
|
| 39 |
results = []
|
| 40 |
for answer, correct, incorrect in zip(answers, correct_answers, incorrect_texts):
|
| 41 |
+
results.append("✔️" if answer == correct else f"✖️ {incorrect}")
|
|
|
|
|
|
|
|
|
|
| 42 |
return "\n".join(results), gr.update(visible=True)
|
| 43 |
|
| 44 |
def reset():
|
| 45 |
+
# Сброс всех радио кнопок и скрытие кнопок и результатов
|
| 46 |
+
return [gr.update(visible=False, value='') for _ in range(max_questions)], gr.update(visible=False), gr.update(visible=True)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
css = """
|
| 49 |
footer {visibility: hidden !important;}
|
|
|
|
| 59 |
submit_button = gr.Button("Проверить", visible=False)
|
| 60 |
back_button = gr.Button("Назад", visible=False)
|
| 61 |
result_output = gr.Markdown(visible=False)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Сохраняем правильные ответы и тексты ошибок в замыканиях
|
| 64 |
+
correct_answers = []
|
| 65 |
+
incorrect_texts = []
|
| 66 |
+
|
| 67 |
+
def show_questions_closure(test_name):
|
| 68 |
+
return show_questions(test_name)
|
| 69 |
+
|
| 70 |
+
def check_closure(answers):
|
| 71 |
+
return check(answers, correct_answers, incorrect_texts)
|
| 72 |
+
|
| 73 |
+
select_button.click(fn=show_questions_closure, inputs=[test_selector],
|
| 74 |
+
outputs=question_blocks + [submit_button, back_button])
|
| 75 |
+
submit_button.click(fn=check_closure, inputs=question_blocks, outputs=[result_output])
|
| 76 |
+
back_button.click(fn=reset, inputs=[], outputs=question_blocks + [result_output, submit_button, test_selector])
|
| 77 |
|
| 78 |
vui.launch()
|