CasperDylan commited on
Commit
d098f3e
·
verified ·
1 Parent(s): 4d1b29a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datetime import datetime
3
+
4
+ # 初始化To-Do List
5
+ todo_list = []
6
+
7
+ def add_todo_item(date, time, title, details, location):
8
+ item = {
9
+ "date": date,
10
+ "time": time,
11
+ "title": title,
12
+ "details": details,
13
+ "location": location,
14
+ "completed": False
15
+ }
16
+ todo_list.append(item)
17
+ return update_view()
18
+
19
+ def delete_todo_item(index):
20
+ del todo_list[index]
21
+ return update_view()
22
+
23
+ def complete_todo_item(index):
24
+ todo_list[index]["completed"] = not todo_list[index]["completed"]
25
+ return update_view()
26
+
27
+ def update_view():
28
+ sorted_list = sorted(todo_list, key=lambda x: (x['date'], x['time']))
29
+ overview = ""
30
+ for i, item in enumerate(sorted_list):
31
+ status = "完成" if item["completed"] else "未完成"
32
+ overview += f"{i+1}. {item['date']} {item['time']} - {item['title']} ({status})\n"
33
+ overview += f" 細節: {item['details']}\n"
34
+ overview += f" 地點: {item['location']}\n"
35
+ overview += f" [刪除] [完成/未完成]\n\n"
36
+ return overview
37
+
38
+ def handle_action(action, index):
39
+ index = int(index) - 1
40
+ if action == "刪除":
41
+ return delete_todo_item(index)
42
+ elif action == "完成/未完成":
43
+ return complete_todo_item(index)
44
+
45
+ with gr.Blocks() as demo:
46
+ with gr.Row():
47
+ with gr.Column():
48
+ date = gr.Date(label="日期")
49
+ time = gr.Time(label="時間")
50
+ title = gr.Textbox(label="事項主題")
51
+ details = gr.Textbox(label="事項細節")
52
+ location = gr.Textbox(label="地點")
53
+ add_button = gr.Button("新增")
54
+ with gr.Column():
55
+ overview = gr.Textbox(label="To-Do List 總覽", interactive=False)
56
+ action = gr.Radio(choices=["刪除", "完成/未完成"], label="動作")
57
+ index = gr.Number(label="項目編號")
58
+ action_button = gr.Button("執行")
59
+
60
+ add_button.click(add_todo_item, [date, time, title, details, location], overview)
61
+ action_button.click(handle_action, [action, index], overview)
62
+
63
+ demo.load(update_view, inputs=None, outputs=overview)
64
+
65
+ demo.launch()