Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
def check(answers,
|
| 30 |
-
questions = data['tests'][test_name]['questions']
|
| 31 |
results = []
|
| 32 |
-
for answer,
|
| 33 |
-
if answer ==
|
| 34 |
-
|
| 35 |
-
else:
|
| 36 |
-
results.append(f"✖️ ({question['incorrect_text']})")
|
| 37 |
-
return "\n".join(results), gr.update(visible=True)
|
| 38 |
|
| 39 |
-
def reset(
|
| 40 |
-
return
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=[
|
| 56 |
-
submit_button.click(check, inputs=questions + [
|
| 57 |
-
back_button.click(reset, inputs=[
|
| 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()
|