Rooni commited on
Commit
65ade22
1 Parent(s): ad0d4c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -41
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 = 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 = """
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
- 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()
 
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()