Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
import json
|
3 |
|
4 |
# JSON с тестами, вопросами, правильными ответами и текстом при неправильном ответе
|
5 |
json_data = """
|
@@ -22,23 +21,21 @@ json_data = """
|
|
22 |
"""
|
23 |
|
24 |
# Загрузка данных из JSON
|
25 |
-
data =
|
26 |
|
27 |
# functions
|
28 |
-
def
|
29 |
-
|
|
|
30 |
|
31 |
-
def check(answers,
|
|
|
32 |
results = []
|
33 |
-
for i, (answer,
|
34 |
-
result = "✔" if answer == correct_answer else f"✖️ ({incorrect_text})"
|
35 |
results.append(f"{i}: {result}")
|
36 |
-
|
37 |
return "\n".join(results)
|
38 |
|
39 |
-
def go_back():
|
40 |
-
return "back"
|
41 |
-
|
42 |
# css
|
43 |
css = """
|
44 |
footer {visibility: hidden !important;}
|
@@ -49,44 +46,41 @@ with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
|
49 |
with gr.Row():
|
50 |
test_selector = gr.Radio(label="Выберите тест", choices=list(data['tests'].keys()))
|
51 |
select_button = gr.Button("Выбрать")
|
52 |
-
|
53 |
-
|
54 |
-
for test_name
|
55 |
-
with gr.Tab(test_name):
|
56 |
-
question_blocks = [
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
radio = gr.Radio(label=question['text'], choices=["А", "Б", "В"], info=question['text'])
|
61 |
-
question_blocks.append(radio)
|
62 |
-
correct_answers.append(question['correct_answer'])
|
63 |
-
incorrect_texts.append(question['incorrect_text'])
|
64 |
submit_button = gr.Button("Проверить")
|
65 |
back_button = gr.Button("Назад")
|
66 |
-
|
67 |
|
68 |
-
|
|
|
69 |
text_output = gr.Markdown("")
|
70 |
|
71 |
-
def
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
return {name: gr.update(visible=False)}
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
test_name = args[0]
|
83 |
-
answers = args[1:-3]
|
84 |
-
for name, question_blocks, correct_answers, incorrect_texts, submit_button, _ in test_question_blocks:
|
85 |
-
if name == test_name:
|
86 |
-
return check(answers, correct_answers, incorrect_texts)
|
87 |
|
88 |
-
for test_name,
|
89 |
-
submit_button.click(
|
|
|
90 |
|
91 |
#end
|
92 |
vui.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
# JSON с тестами, вопросами, правильными ответами и текстом при неправильном ответе
|
4 |
json_data = """
|
|
|
21 |
"""
|
22 |
|
23 |
# Загрузка данных из JSON
|
24 |
+
data = gr.Interface.load(json_data)
|
25 |
|
26 |
# functions
|
27 |
+
def show_test(test_name):
|
28 |
+
test_data = data['tests'][test_name]
|
29 |
+
return test_data, test_name
|
30 |
|
31 |
+
def check(answers, test_name):
|
32 |
+
test_data = data['tests'][test_name]
|
33 |
results = []
|
34 |
+
for i, (answer, question) in enumerate(zip(answers, test_data['questions']), start=1):
|
35 |
+
result = "✔" if answer == question['correct_answer'] else f"✖️ ({question['incorrect_text']})"
|
36 |
results.append(f"{i}: {result}")
|
|
|
37 |
return "\n".join(results)
|
38 |
|
|
|
|
|
|
|
39 |
# css
|
40 |
css = """
|
41 |
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 |
+
test_tabs = {}
|
51 |
+
for test_name in data['tests']:
|
52 |
+
with gr.Tab(test_name, visible=False) as tab:
|
53 |
+
question_blocks = [
|
54 |
+
gr.Radio(label=question['text'], choices=["А", "Б", "В"], info=question['text'])
|
55 |
+
for question in data['tests'][test_name]['questions']
|
56 |
+
]
|
|
|
|
|
|
|
|
|
57 |
submit_button = gr.Button("Проверить")
|
58 |
back_button = gr.Button("Назад")
|
59 |
+
test_tabs[test_name] = (tab, question_blocks, submit_button, back_button)
|
60 |
|
61 |
+
results_tab = gr.Tab("Результаты")
|
62 |
+
with results_tab:
|
63 |
text_output = gr.Markdown("")
|
64 |
|
65 |
+
def on_select(test_data, test_name):
|
66 |
+
tab, question_blocks, _, _ = test_tabs[test_name]
|
67 |
+
tab.update(visible=True)
|
68 |
+
test_selector.update(visible=False)
|
69 |
+
select_button.update(visible=False)
|
70 |
+
return ([gr.update(value='') for _ in question_blocks], test_name)
|
|
|
71 |
|
72 |
+
def on_back(test_name):
|
73 |
+
tab, _, _, _ = test_tabs[test_name]
|
74 |
+
tab.update(visible=False)
|
75 |
+
test_selector.update(visible=True)
|
76 |
+
select_button.update(visible=True)
|
77 |
+
return gr.update(value='')
|
78 |
|
79 |
+
select_button.click(show_test, inputs=[test_selector], outputs=[gr.Group(*[block for _, blocks, _, _ in test_tabs.values() for block in blocks]), test_selector])
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
for test_name, (tab, question_blocks, submit_button, back_button) in test_tabs.items():
|
82 |
+
submit_button.click(check, inputs=question_blocks, outputs=[text_output])
|
83 |
+
back_button.click(on_back, inputs=[test_name], outputs=[test_selector])
|
84 |
|
85 |
#end
|
86 |
vui.launch()
|