Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
import uuid
|
3 |
|
4 |
# functions
|
5 |
-
def check(answers, correct_answers
|
6 |
-
if session_token in completed_sessions:
|
7 |
-
raise gr.Error("Вы уже проверили свои ответы!")
|
8 |
-
|
9 |
if not all(answers):
|
10 |
raise gr.Error("Выберите все ответы!")
|
11 |
|
@@ -14,16 +10,31 @@ def check(answers, correct_answers, session_token):
|
|
14 |
result = "✔" if answer == correct_answer else "✖️"
|
15 |
results.append(f"{i}: {result}")
|
16 |
|
17 |
-
completed_sessions.add(session_token) # Добавляем токен сессии в список выполненных
|
18 |
return "\n".join(results)
|
19 |
|
20 |
# css
|
21 |
css = """
|
22 |
footer {visibility: hidden !important;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"""
|
24 |
|
25 |
# ui
|
26 |
-
with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
27 |
question_blocks = []
|
28 |
correct_answers = ["А", "Б"] # Предполагаем, что это правильные ответы
|
29 |
for i, correct_answer in enumerate(correct_answers, start=1):
|
@@ -31,19 +42,18 @@ with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
|
31 |
with gr.Column():
|
32 |
question = gr.Radio(label=f"Вопрос {i}", choices=["А", "Б", "В"])
|
33 |
question_blocks.append(question)
|
34 |
-
|
35 |
text_button = gr.Button("Проверить", variant='primary')
|
36 |
with gr.Tab("Результаты"):
|
37 |
text_output = gr.Markdown("")
|
38 |
|
39 |
-
# Множество токенов сессий пользователей, которые уже сделали запрос
|
40 |
-
completed_sessions = set()
|
41 |
-
|
42 |
def on_click(*args):
|
43 |
-
|
44 |
-
return check(args, correct_answers, session_token)
|
45 |
|
46 |
text_button.click(on_click, inputs=question_blocks, outputs=[text_output], queue=False)
|
47 |
|
|
|
|
|
|
|
48 |
#end
|
49 |
vui.queue(api_open=False).launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
# functions
|
4 |
+
def check(answers, correct_answers):
|
|
|
|
|
|
|
5 |
if not all(answers):
|
6 |
raise gr.Error("Выберите все ответы!")
|
7 |
|
|
|
10 |
result = "✔" if answer == correct_answer else "✖️"
|
11 |
results.append(f"{i}: {result}")
|
12 |
|
|
|
13 |
return "\n".join(results)
|
14 |
|
15 |
# css
|
16 |
css = """
|
17 |
footer {visibility: hidden !important;}
|
18 |
+
button, .output-container {transition: opacity 1s ease-in-out;}
|
19 |
+
button.hide, .output-container.hide {opacity: 0; pointer-events: none; height: 0; overflow: hidden;}
|
20 |
+
"""
|
21 |
+
|
22 |
+
# javascript
|
23 |
+
javascript = """
|
24 |
+
document.addEventListener('DOMContentLoaded', function() {
|
25 |
+
const checkButton = document.querySelector('button');
|
26 |
+
const outputContainer = document.querySelector('.output-container');
|
27 |
+
checkButton.addEventListener('click', function() {
|
28 |
+
checkButton.classList.add('hide');
|
29 |
+
setTimeout(function() {
|
30 |
+
outputContainer.classList.remove('hide');
|
31 |
+
}, 1000); // wait for the button hide animation to finish
|
32 |
+
});
|
33 |
+
});
|
34 |
"""
|
35 |
|
36 |
# ui
|
37 |
+
with gr.Blocks(css=css, javascript=javascript, theme='YTheme/TehnoX') as vui:
|
38 |
question_blocks = []
|
39 |
correct_answers = ["А", "Б"] # Предполагаем, что это правильные ответы
|
40 |
for i, correct_answer in enumerate(correct_answers, start=1):
|
|
|
42 |
with gr.Column():
|
43 |
question = gr.Radio(label=f"Вопрос {i}", choices=["А", "Б", "В"])
|
44 |
question_blocks.append(question)
|
45 |
+
|
46 |
text_button = gr.Button("Проверить", variant='primary')
|
47 |
with gr.Tab("Результаты"):
|
48 |
text_output = gr.Markdown("")
|
49 |
|
|
|
|
|
|
|
50 |
def on_click(*args):
|
51 |
+
return check(args, correct_answers)
|
|
|
52 |
|
53 |
text_button.click(on_click, inputs=question_blocks, outputs=[text_output], queue=False)
|
54 |
|
55 |
+
# Добавляем класс 'hide' к контейнеру вывода по умолчанию, чтобы он был скрыт при загрузке страницы
|
56 |
+
text_output.style(container_class='hide')
|
57 |
+
|
58 |
#end
|
59 |
vui.queue(api_open=False).launch()
|