Rooni commited on
Commit
ad0d4c6
·
1 Parent(s): 6b6a1ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -19
app.py CHANGED
@@ -1,13 +1,23 @@
1
  import gradio as gr
2
  import json
3
 
4
- # JSON с вопросами и правильными ответами
5
  json_data = """
6
  {
7
- "questions": [
8
- {"text": "Выберите А", "correct_answer": "А"},
9
- {"text": "Выберите Б", "correct_answer": "Б"}
10
- ]
 
 
 
 
 
 
 
 
 
 
11
  }
12
  """
13
 
@@ -15,16 +25,19 @@ json_data = """
15
  data = json.loads(json_data)
16
 
17
  # functions
18
- def check(answers, correct_answers):
 
 
 
19
  results = []
20
- for i, (answer, correct_answer) in enumerate(zip(answers, correct_answers), start=1):
21
- result = "✔" if answer == correct_answer else "✖️"
22
  results.append(f"{i}: {result}")
23
 
24
  return "\n".join(results)
25
 
26
- # Список правильных ответов
27
- correct_answers = [question['correct_answer'] for question in data['questions']]
28
 
29
  # css
30
  css = """
@@ -33,21 +46,47 @@ footer {visibility: hidden !important;}
33
 
34
  # ui
35
  with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
36
- question_blocks = []
37
- for i, question in enumerate(data['questions'], start=1):
38
- with gr.Row():
39
- with gr.Column():
40
- radio = gr.Radio(label=f"Вопрос {i}", choices=["А", "Б", "В"], info=question['text'])
 
 
 
 
 
 
 
41
  question_blocks.append(radio)
 
 
 
 
 
42
 
43
- text_button = gr.Button("Проверить", variant='primary')
44
  with gr.Tab("Результаты"):
45
  text_output = gr.Markdown("")
46
 
47
- def on_click(*args):
48
- return check(args, correct_answers)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- text_button.click(on_click, inputs=question_blocks, outputs=[text_output], queue=False)
 
51
 
52
  #end
53
  vui.launch()
 
1
  import gradio as gr
2
  import json
3
 
4
+ # JSON с тестами, вопросами, правильными ответами и текстом при неправильном ответе
5
  json_data = """
6
  {
7
+ "tests": {
8
+ "test1": {
9
+ "questions": [
10
+ {"text": "Выберите А", "correct_answer": "А", "incorrect_text": "Правильный ответ - А."},
11
+ {"text": "Выберите Б", "correct_answer": "Б", "incorrect_text": "Правильный ответ - Б."}
12
+ ]
13
+ },
14
+ "test2": {
15
+ "questions": [
16
+ {"text": "Выберите В", "correct_answer": "В", "incorrect_text": "Правильный ответ - В."},
17
+ {"text": "Выберите А", "correct_answer": "А", "incorrect_text": "Правильный ответ - А."}
18
+ ]
19
+ }
20
+ }
21
  }
22
  """
23
 
 
25
  data = json.loads(json_data)
26
 
27
  # functions
28
+ def select_test(test_name):
29
+ return test_name
30
+
31
+ def check(answers, correct_answers, incorrect_texts):
32
  results = []
33
+ for i, (answer, correct_answer, incorrect_text) in enumerate(zip(answers, correct_answers, incorrect_texts), start=1):
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 = """
 
46
 
47
  # ui
48
  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
+ test_question_blocks = []
54
+ for test_name, test_data in data['tests'].items():
55
+ with gr.Tab(test_name):
56
+ question_blocks = []
57
+ correct_answers = []
58
+ incorrect_texts = []
59
+ for question in test_data['questions']:
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
+ test_question_blocks.append((test_name, question_blocks, correct_answers, incorrect_texts, submit_button, back_button))
67
 
 
68
  with gr.Tab("Результаты"):
69
  text_output = gr.Markdown("")
70
 
71
+ def on_select_test(test_name):
72
+ for name, _, _, _, _, back_button in test_question_blocks:
73
+ back_button.click(go_back, inputs=[], outputs=[test_selector])
74
+ if name == test_name:
75
+ return {name: gr.update(visible=True)}
76
+ else:
77
+ return {name: gr.update(visible=False)}
78
+
79
+ select_button.click(on_select_test, inputs=[test_selector], outputs=[test_selector])
80
+
81
+ def on_submit(*args):
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, question_blocks, correct_answers, incorrect_texts, submit_button, _ in test_question_blocks:
89
+ submit_button.click(on_submit, inputs=[test_name] + question_blocks + [submit_button], outputs=[text_output])
90
 
91
  #end
92
  vui.launch()