Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
|
4 |
+
# 定義座位排法邏輯
|
5 |
+
def arrange_seating(input_data):
|
6 |
+
# 將輸入的班級座號與姓名轉換為列表
|
7 |
+
students = [line.strip() for line in input_data.strip().split('\n') if line.strip()]
|
8 |
+
|
9 |
+
# 檢查學生數量
|
10 |
+
total_seats = 32
|
11 |
+
empty_seats = total_seats - len(students)
|
12 |
+
|
13 |
+
# 如果學生數量少於32,補充空位
|
14 |
+
if empty_seats > 0:
|
15 |
+
students.extend(['空位'] * empty_seats)
|
16 |
+
|
17 |
+
# 隨機打亂學生列表
|
18 |
+
random.shuffle(students)
|
19 |
+
|
20 |
+
# 排座位 4 排,每排 8 個座位
|
21 |
+
seating_arrangement = []
|
22 |
+
for i in range(4):
|
23 |
+
row = students[i*8:(i+1)*8]
|
24 |
+
seating_arrangement.append(row)
|
25 |
+
|
26 |
+
# 將結果轉換成表格顯示
|
27 |
+
seating_table = "\n".join(["\t".join(row) for row in seating_arrangement])
|
28 |
+
|
29 |
+
return seating_table
|
30 |
+
|
31 |
+
# 使用 Gradio 構建介面
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("## 座位排法系統")
|
34 |
+
|
35 |
+
input_text = gr.Textbox(lines=10, label="請輸入班級座號與姓名(每行一個)", placeholder="1 張三\n2 李四\n3 王五")
|
36 |
+
arrange_button = gr.Button("排座位")
|
37 |
+
result = gr.Textbox(label="座位安排結果", lines=10)
|
38 |
+
|
39 |
+
arrange_button.click(arrange_seating, inputs=input_text, outputs=result)
|
40 |
+
|
41 |
+
# 啟動 Gradio 應用
|
42 |
+
demo.launch()
|