Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,16 @@ 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,7 +21,7 @@ def load_data():
|
|
| 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")}
|
|
@@ -31,30 +31,35 @@ def save_activity(activity):
|
|
| 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 |
df = pd.DataFrame(data)
|
| 38 |
if df.empty:
|
| 39 |
-
return
|
| 40 |
if not show_all:
|
| 41 |
-
df = df.tail(5)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
del data[row_index]
|
| 51 |
-
with open(data_file, "w") as file:
|
| 52 |
-
json.dump(data, file)
|
| 53 |
-
return "記錄刪除成功", update_activity_display()
|
| 54 |
-
else:
|
| 55 |
-
return "錯誤: 行號超出範圍", update_activity_display()
|
| 56 |
-
except ValueError:
|
| 57 |
-
return "錯誤: 請輸入有效的數字行號", update_activity_display()
|
| 58 |
|
| 59 |
# 主界面
|
| 60 |
def main_interface():
|
|
@@ -77,58 +82,61 @@ def main_interface():
|
|
| 77 |
.btn-half { background-color: #FFD700; } /* Gold */
|
| 78 |
.btn-no-eat { background-color: #D3D3D3; } /* LightGray */
|
| 79 |
"""
|
|
|
|
| 80 |
with gr.Blocks(css=custom_css) as app:
|
| 81 |
-
gr.Markdown("## 🐾
|
| 82 |
|
| 83 |
-
#
|
| 84 |
with gr.Row():
|
| 85 |
poop_btn = gr.Button("💩 拉屎", elem_classes="custom-btn btn-poop")
|
| 86 |
pee_btn = gr.Button("💦 尿尿", elem_classes="custom-btn btn-pee")
|
| 87 |
eat_btn = gr.Button("🍽️ 全吃了", elem_classes="custom-btn btn-eat")
|
| 88 |
half_eat_btn = gr.Button("🍛 吃了一半", elem_classes="custom-btn btn-half")
|
| 89 |
-
no_eat_btn = gr.Button("🥄
|
| 90 |
-
|
| 91 |
-
#
|
| 92 |
-
gr.Markdown("### 📊
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
return app
|
| 132 |
|
| 133 |
-
|
|
|
|
| 134 |
main_interface().launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 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")}
|
|
|
|
| 31 |
json.dump(data, file)
|
| 32 |
return update_activity_display()
|
| 33 |
|
| 34 |
+
# 删除记录功能
|
| 35 |
+
def delete_activity(index):
|
| 36 |
+
data = load_data()
|
| 37 |
+
try:
|
| 38 |
+
index = int(index)
|
| 39 |
+
if 0 <= index < len(data):
|
| 40 |
+
del data[index]
|
| 41 |
+
with open(data_file, "w") as file:
|
| 42 |
+
json.dump(data, file)
|
| 43 |
+
except:
|
| 44 |
+
pass
|
| 45 |
+
return update_activity_display()
|
| 46 |
+
|
| 47 |
+
# 更新显示活动记录 (仅显示最近 5 条)
|
| 48 |
def update_activity_display(show_all=False):
|
| 49 |
data = load_data()
|
| 50 |
df = pd.DataFrame(data)
|
| 51 |
if df.empty:
|
| 52 |
+
return [], []
|
| 53 |
if not show_all:
|
| 54 |
+
df = df.tail(5).reset_index(drop=True)
|
| 55 |
+
|
| 56 |
+
# 构建展示用的行数据和删除按钮
|
| 57 |
+
rows = []
|
| 58 |
+
delete_buttons = []
|
| 59 |
+
for i, row in df.iterrows():
|
| 60 |
+
rows.append(f"{row['活動']} - {row['時間']}")
|
| 61 |
+
delete_buttons.append(gr.Button(value="❌", elem_id=f"delete_{i}"))
|
| 62 |
+
return rows, delete_buttons
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# 主界面
|
| 65 |
def main_interface():
|
|
|
|
| 82 |
.btn-half { background-color: #FFD700; } /* Gold */
|
| 83 |
.btn-no-eat { background-color: #D3D3D3; } /* LightGray */
|
| 84 |
"""
|
| 85 |
+
|
| 86 |
with gr.Blocks(css=custom_css) as app:
|
| 87 |
+
gr.Markdown("## 🐾 狗狗活动记录器")
|
| 88 |
|
| 89 |
+
# 记录活动按钮 (不同颜色 + icon)
|
| 90 |
with gr.Row():
|
| 91 |
poop_btn = gr.Button("💩 拉屎", elem_classes="custom-btn btn-poop")
|
| 92 |
pee_btn = gr.Button("💦 尿尿", elem_classes="custom-btn btn-pee")
|
| 93 |
eat_btn = gr.Button("🍽️ 全吃了", elem_classes="custom-btn btn-eat")
|
| 94 |
half_eat_btn = gr.Button("🍛 吃了一半", elem_classes="custom-btn btn-half")
|
| 95 |
+
no_eat_btn = gr.Button("🥄 没怎么吃", elem_classes="custom-btn btn-no-eat")
|
| 96 |
+
|
| 97 |
+
# 活动记录部分
|
| 98 |
+
gr.Markdown("### 📊 最近活动记录 (最多显示最近5条)")
|
| 99 |
+
record_list = gr.State([]) # 存储记录
|
| 100 |
+
delete_buttons = gr.State([])
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
record_display = gr.Column()
|
| 104 |
+
delete_button_display = gr.Column()
|
| 105 |
+
|
| 106 |
+
# 显示更多按钮
|
| 107 |
+
show_more_btn = gr.Button("显示更多记录")
|
| 108 |
+
show_less_btn = gr.Button("显示最近5条", visible=False)
|
| 109 |
+
|
| 110 |
+
# 更新显示逻辑
|
| 111 |
+
def refresh_display(show_all=False):
|
| 112 |
+
rows, buttons = update_activity_display(show_all)
|
| 113 |
+
return rows, buttons, gr.update(visible=not show_all), gr.update(visible=show_all)
|
| 114 |
+
|
| 115 |
+
def on_delete(index):
|
| 116 |
+
delete_activity(index)
|
| 117 |
+
return refresh_display()
|
| 118 |
+
|
| 119 |
+
# 添加活动逻辑
|
| 120 |
+
poop_btn.click(lambda: save_activity("拉屎"), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 121 |
+
pee_btn.click(lambda: save_activity("尿尿"), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 122 |
+
eat_btn.click(lambda: save_activity("全吃了"), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 123 |
+
half_eat_btn.click(lambda: save_activity("吃了一半"), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 124 |
+
no_eat_btn.click(lambda: save_activity("没怎么吃"), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 125 |
+
|
| 126 |
+
# 显示更多/更少逻辑
|
| 127 |
+
show_more_btn.click(lambda: refresh_display(show_all=True), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 128 |
+
show_less_btn.click(lambda: refresh_display(show_all=False), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 129 |
+
|
| 130 |
+
# 动态绑定删除按钮事件
|
| 131 |
+
for i in range(5): # 最多5条删除按钮
|
| 132 |
+
delete_button = gr.Button(visible=False, value="❌")
|
| 133 |
+
delete_button.click(lambda idx=i: on_delete(idx), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 134 |
+
|
| 135 |
+
# 初始显示
|
| 136 |
+
app.load(lambda: refresh_display(), outputs=[record_list, delete_buttons, show_more_btn, show_less_btn])
|
| 137 |
|
| 138 |
return app
|
| 139 |
|
| 140 |
+
|
| 141 |
+
# 启动应用
|
| 142 |
main_interface().launch(share=True, server_name="0.0.0.0", server_port=7860)
|