Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import datetime
|
| 3 |
-
import pandas as pd
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
|
|
@@ -23,58 +22,46 @@ def load_data():
|
|
| 23 |
|
| 24 |
# 保存活动记录
|
| 25 |
def save_activity(activity):
|
| 26 |
-
timestamp = datetime.datetime.now()
|
| 27 |
-
record = {"活動": activity, "時間": timestamp
|
| 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 delete_activity(index):
|
| 36 |
data = load_data()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
with open(data_file, "w") as file:
|
| 42 |
-
json.dump(data, file)
|
| 43 |
-
except:
|
| 44 |
-
pass
|
| 45 |
return update_activity_display()
|
| 46 |
|
| 47 |
-
#
|
| 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 |
-
|
| 55 |
-
|
| 56 |
-
# 构建展示用的行数据和删除按钮
|
| 57 |
rows = []
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
delete_buttons.append(gr.Button(value="❌", elem_id=f"delete_{i}"))
|
| 62 |
-
return rows, delete_buttons
|
| 63 |
|
| 64 |
# 主界面
|
| 65 |
def main_interface():
|
| 66 |
init_data_file()
|
|
|
|
| 67 |
custom_css = """
|
| 68 |
.custom-btn {
|
| 69 |
-
width: 100%;
|
| 70 |
padding: 10px;
|
| 71 |
-
margin: 5px
|
| 72 |
color: white;
|
| 73 |
border: none;
|
| 74 |
font-size: 16px;
|
| 75 |
-
text-align: center;
|
| 76 |
-
cursor: pointer;
|
| 77 |
border-radius: 5px;
|
|
|
|
| 78 |
}
|
| 79 |
.btn-poop { background-color: #FF6347; } /* Tomato */
|
| 80 |
.btn-pee { background-color: #4682B4; } /* SteelBlue */
|
|
@@ -84,9 +71,9 @@ def main_interface():
|
|
| 84 |
"""
|
| 85 |
|
| 86 |
with gr.Blocks(css=custom_css) as app:
|
| 87 |
-
gr.Markdown("## 🐾
|
| 88 |
|
| 89 |
-
#
|
| 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")
|
|
@@ -94,46 +81,39 @@ def main_interface():
|
|
| 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("###
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def on_delete(index):
|
| 116 |
delete_activity(index)
|
| 117 |
-
return
|
| 118 |
-
|
| 119 |
-
#
|
| 120 |
-
|
| 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 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import datetime
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 22 |
|
| 23 |
# 保存活动记录
|
| 24 |
def save_activity(activity):
|
| 25 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 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 |
+
data = data[-5:] # 取最近5条记录
|
|
|
|
|
|
|
| 47 |
rows = []
|
| 48 |
+
for i, record in enumerate(data):
|
| 49 |
+
rows.append([record["活動"], record["時間"], i]) # i 为删除按钮的索引
|
| 50 |
+
return rows
|
|
|
|
|
|
|
| 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 |
border-radius: 5px;
|
| 64 |
+
cursor: pointer;
|
| 65 |
}
|
| 66 |
.btn-poop { background-color: #FF6347; } /* Tomato */
|
| 67 |
.btn-pee { background-color: #4682B4; } /* SteelBlue */
|
|
|
|
| 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")
|
|
|
|
| 81 |
half_eat_btn = gr.Button("🍛 吃了一半", elem_classes="custom-btn btn-half")
|
| 82 |
no_eat_btn = gr.Button("🥄 没怎么吃", elem_classes="custom-btn btn-no-eat")
|
| 83 |
|
| 84 |
+
# 记录显示区域
|
| 85 |
+
gr.Markdown("### 📋 最近活動記錄")
|
| 86 |
+
record_table = gr.Dataframe(headers=["活動", "時間", "刪除"], interactive=False, height=200)
|
| 87 |
+
show_more_btn = gr.Button("顯示更多記錄")
|
| 88 |
+
show_less_btn = gr.Button("顯示最近5條", visible=False)
|
| 89 |
|
| 90 |
+
# 刷新记录显示
|
| 91 |
+
def refresh_records(show_all=False):
|
| 92 |
+
records = update_activity_display(show_all=show_all)
|
| 93 |
+
return records, gr.update(visible=not show_all), gr.update(visible=show_all)
|
| 94 |
|
| 95 |
+
# 删除指定记录
|
| 96 |
+
def delete_record(index):
|
| 97 |
+
delete_activity(index)
|
| 98 |
+
return refresh_records()
|
| 99 |
|
| 100 |
+
# 按钮点击逻辑
|
| 101 |
+
poop_btn.click(lambda: save_activity("拉屎"), outputs=[record_table, show_more_btn, show_less_btn])
|
| 102 |
+
pee_btn.click(lambda: save_activity("尿尿"), outputs=[record_table, show_more_btn, show_less_btn])
|
| 103 |
+
eat_btn.click(lambda: save_activity("全吃了"), outputs=[record_table, show_more_btn, show_less_btn])
|
| 104 |
+
half_eat_btn.click(lambda: save_activity("吃了一半"), outputs=[record_table, show_more_btn, show_less_btn])
|
| 105 |
+
no_eat_btn.click(lambda: save_activity("沒怎麼吃"), outputs=[record_table, show_more_btn, show_less_btn])
|
| 106 |
|
| 107 |
+
show_more_btn.click(lambda: refresh_records(show_all=True), outputs=[record_table, show_more_btn, show_less_btn])
|
| 108 |
+
show_less_btn.click(lambda: refresh_records(show_all=False), outputs=[record_table, show_more_btn, show_less_btn])
|
| 109 |
+
|
| 110 |
+
# 删除记录按钮
|
| 111 |
def on_delete(index):
|
| 112 |
delete_activity(index)
|
| 113 |
+
return refresh_records()
|
| 114 |
+
|
| 115 |
+
# 初始化加载记录
|
| 116 |
+
app.load(lambda: refresh_records(), outputs=[record_table, show_more_btn, show_less_btn])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
return app
|
| 119 |
|