Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import datetime
|
|
|
3 |
import json
|
4 |
import os
|
5 |
|
6 |
-
#
|
7 |
data_file = "dog_activity_data.json"
|
8 |
|
9 |
-
#
|
10 |
def init_data_file():
|
11 |
if not os.path.exists(data_file):
|
12 |
with open(data_file, "w") as file:
|
13 |
json.dump([], file)
|
14 |
|
15 |
-
#
|
16 |
def load_data():
|
17 |
try:
|
18 |
with open(data_file, "r") as file:
|
@@ -20,48 +21,59 @@ def load_data():
|
|
20 |
except:
|
21 |
return []
|
22 |
|
23 |
-
#
|
24 |
def save_activity(activity):
|
25 |
-
timestamp = datetime.datetime.now()
|
26 |
-
record = {"活動": activity, "時間": timestamp}
|
27 |
data = load_data()
|
28 |
data.append(record)
|
29 |
with open(data_file, "w") as file:
|
30 |
json.dump(data, file)
|
31 |
return update_activity_display()
|
32 |
|
33 |
-
#
|
34 |
-
def delete_activity(index):
|
35 |
-
data = load_data()
|
36 |
-
if 0 <= index < len(data):
|
37 |
-
data.pop(index)
|
38 |
-
with open(data_file, "w") as file:
|
39 |
-
json.dump(data, file)
|
40 |
-
return update_activity_display()
|
41 |
-
|
42 |
-
# 显示最近的活动记录 (限制为5条或全部显示)
|
43 |
def update_activity_display(show_all=False):
|
44 |
data = load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
if not show_all:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# 主界面
|
53 |
def main_interface():
|
54 |
init_data_file()
|
55 |
-
|
56 |
custom_css = """
|
57 |
.custom-btn {
|
|
|
58 |
padding: 10px;
|
59 |
-
margin: 5px;
|
60 |
color: white;
|
61 |
border: none;
|
62 |
font-size: 16px;
|
63 |
-
|
64 |
cursor: pointer;
|
|
|
65 |
}
|
66 |
.btn-poop { background-color: #FF6347; } /* Tomato */
|
67 |
.btn-pee { background-color: #4682B4; } /* SteelBlue */
|
@@ -69,49 +81,58 @@ def main_interface():
|
|
69 |
.btn-half { background-color: #FFD700; } /* Gold */
|
70 |
.btn-no-eat { background-color: #D3D3D3; } /* LightGray */
|
71 |
"""
|
72 |
-
|
73 |
with gr.Blocks(css=custom_css) as app:
|
74 |
gr.Markdown("## 🐾 狗狗活動記錄器")
|
75 |
|
76 |
-
#
|
77 |
with gr.Row():
|
78 |
poop_btn = gr.Button("💩 拉屎", elem_classes="custom-btn btn-poop")
|
79 |
pee_btn = gr.Button("💦 尿尿", elem_classes="custom-btn btn-pee")
|
80 |
eat_btn = gr.Button("🍽️ 全吃了", elem_classes="custom-btn btn-eat")
|
81 |
half_eat_btn = gr.Button("🍛 吃了一半", elem_classes="custom-btn btn-half")
|
82 |
-
no_eat_btn = gr.Button("🥄
|
83 |
|
84 |
-
#
|
85 |
-
gr.Markdown("###
|
86 |
-
|
|
|
|
|
87 |
show_more_btn = gr.Button("顯示更多記錄")
|
88 |
show_less_btn = gr.Button("顯示最近5條", visible=False)
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
return app
|
114 |
|
115 |
-
|
116 |
-
# 启动应用
|
117 |
main_interface().launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
import gradio as gr
|
2 |
import datetime
|
3 |
+
import pandas as pd
|
4 |
import json
|
5 |
import os
|
6 |
|
7 |
+
# 數據存儲文件
|
8 |
data_file = "dog_activity_data.json"
|
9 |
|
10 |
+
# 初始化數據文件
|
11 |
def init_data_file():
|
12 |
if not os.path.exists(data_file):
|
13 |
with open(data_file, "w") as file:
|
14 |
json.dump([], file)
|
15 |
|
16 |
+
# 加載活動記錄
|
17 |
def load_data():
|
18 |
try:
|
19 |
with open(data_file, "r") as file:
|
|
|
21 |
except:
|
22 |
return []
|
23 |
|
24 |
+
# 保存活動記錄
|
25 |
def save_activity(activity):
|
26 |
+
timestamp = datetime.datetime.now()
|
27 |
+
record = {"活動": activity, "時間": timestamp.strftime("%Y-%m-%d %H:%M:%S")}
|
28 |
data = load_data()
|
29 |
data.append(record)
|
30 |
with open(data_file, "w") as file:
|
31 |
json.dump(data, file)
|
32 |
return update_activity_display()
|
33 |
|
34 |
+
# 更新顯示活動記錄 (增加索引列)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def update_activity_display(show_all=False):
|
36 |
data = load_data()
|
37 |
+
if not data:
|
38 |
+
return pd.DataFrame(columns=["#(行號)", "活動", "時間"])
|
39 |
+
|
40 |
+
# 添加索引列
|
41 |
+
df = pd.DataFrame(data)
|
42 |
+
df.insert(0, "#(行號)", range(0, len(df)))
|
43 |
+
|
44 |
if not show_all:
|
45 |
+
df = df.tail(5) # 顯示最近 5 條
|
46 |
+
return df
|
47 |
+
|
48 |
+
# 刪除記錄功能
|
49 |
+
def delete_activity(row_index):
|
50 |
+
data = load_data()
|
51 |
+
try:
|
52 |
+
row_index = int(row_index)
|
53 |
+
if 0 <= row_index < len(data):
|
54 |
+
del data[row_index]
|
55 |
+
with open(data_file, "w") as file:
|
56 |
+
json.dump(data, file)
|
57 |
+
return "記錄刪除成功", update_activity_display()
|
58 |
+
else:
|
59 |
+
return "錯誤: 行號超出範圍", update_activity_display()
|
60 |
+
except ValueError:
|
61 |
+
return "錯誤: 請輸入有效的數字行號", update_activity_display()
|
62 |
|
63 |
# 主界面
|
64 |
def main_interface():
|
65 |
init_data_file()
|
|
|
66 |
custom_css = """
|
67 |
.custom-btn {
|
68 |
+
width: 100%;
|
69 |
padding: 10px;
|
70 |
+
margin: 5px 0;
|
71 |
color: white;
|
72 |
border: none;
|
73 |
font-size: 16px;
|
74 |
+
text-align: center;
|
75 |
cursor: pointer;
|
76 |
+
border-radius: 5px;
|
77 |
}
|
78 |
.btn-poop { background-color: #FF6347; } /* Tomato */
|
79 |
.btn-pee { background-color: #4682B4; } /* SteelBlue */
|
|
|
81 |
.btn-half { background-color: #FFD700; } /* Gold */
|
82 |
.btn-no-eat { background-color: #D3D3D3; } /* LightGray */
|
83 |
"""
|
|
|
84 |
with gr.Blocks(css=custom_css) as app:
|
85 |
gr.Markdown("## 🐾 狗狗活動記錄器")
|
86 |
|
87 |
+
# 記錄活動按鈕 (不同顏色 + icon)
|
88 |
with gr.Row():
|
89 |
poop_btn = gr.Button("💩 拉屎", elem_classes="custom-btn btn-poop")
|
90 |
pee_btn = gr.Button("💦 尿尿", elem_classes="custom-btn btn-pee")
|
91 |
eat_btn = gr.Button("🍽️ 全吃了", elem_classes="custom-btn btn-eat")
|
92 |
half_eat_btn = gr.Button("🍛 吃了一半", elem_classes="custom-btn btn-half")
|
93 |
+
no_eat_btn = gr.Button("🥄 沒怎麼吃", elem_classes="custom-btn btn-no-eat")
|
94 |
|
95 |
+
# 活動記錄表格
|
96 |
+
gr.Markdown("### 📊 最近活動記錄 (僅顯示最近5條)")
|
97 |
+
data_table = gr.Dataframe(update_activity_display(), interactive=False)
|
98 |
+
|
99 |
+
# 顯示更多按鈕
|
100 |
show_more_btn = gr.Button("顯示更多記錄")
|
101 |
show_less_btn = gr.Button("顯示最近5條", visible=False)
|
102 |
|
103 |
+
# 刪除記錄功能
|
104 |
+
gr.Markdown("### 🗑️ 刪除記錄")
|
105 |
+
row_to_delete = gr.Textbox(label="輸入要刪除的行號 (從 0 開始)")
|
106 |
+
delete_btn = gr.Button("刪除記錄")
|
107 |
+
delete_msg = gr.Textbox(label="刪除狀態", interactive=False)
|
108 |
+
|
109 |
+
# 綁定活動按鈕邏輯
|
110 |
+
poop_btn.click(lambda: save_activity("拉屎"), outputs=[data_table])
|
111 |
+
pee_btn.click(lambda: save_activity("尿尿"), outputs=[data_table])
|
112 |
+
eat_btn.click(lambda: save_activity("全吃了"), outputs=[data_table])
|
113 |
+
half_eat_btn.click(lambda: save_activity("吃了一半"), outputs=[data_table])
|
114 |
+
no_eat_btn.click(lambda: save_activity("沒怎麼吃"), outputs=[data_table])
|
115 |
+
|
116 |
+
# 綁定顯示更多邏輯
|
117 |
+
show_more_btn.click(
|
118 |
+
lambda: (update_activity_display(show_all=True), gr.update(visible=False), gr.update(visible=True)),
|
119 |
+
inputs=[],
|
120 |
+
outputs=[data_table, show_more_btn, show_less_btn]
|
121 |
+
)
|
122 |
+
show_less_btn.click(
|
123 |
+
lambda: (update_activity_display(show_all=False), gr.update(visible=True), gr.update(visible=False)),
|
124 |
+
inputs=[],
|
125 |
+
outputs=[data_table, show_more_btn, show_less_btn]
|
126 |
+
)
|
127 |
+
|
128 |
+
# 刪除記錄邏輯
|
129 |
+
delete_btn.click(
|
130 |
+
delete_activity,
|
131 |
+
inputs=[row_to_delete],
|
132 |
+
outputs=[delete_msg, data_table]
|
133 |
+
)
|
134 |
|
135 |
return app
|
136 |
|
137 |
+
# 啟動應用
|
|
|
138 |
main_interface().launch(share=True, server_name="0.0.0.0", server_port=7860)
|