Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,64 @@ import gradio as gr
|
|
| 2 |
import datetime
|
| 3 |
import pandas as pd
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
| 7 |
data_file = "dog_activity_data.json"
|
| 8 |
-
users = {"admin": "password"} # 模擬用戶數據
|
| 9 |
|
| 10 |
# 初始化數據文件
|
| 11 |
def init_data_file():
|
| 12 |
-
|
| 13 |
-
with open(data_file, "r") as file:
|
| 14 |
-
json.load(file)
|
| 15 |
-
except FileNotFoundError:
|
| 16 |
with open(data_file, "w") as file:
|
| 17 |
json.dump([], file)
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def save_activity(activity):
|
| 21 |
timestamp = datetime.datetime.now()
|
| 22 |
record = {"活動": activity, "時間": timestamp.strftime("%Y-%m-%d %H:%M:%S")}
|
| 23 |
-
|
| 24 |
-
data = json.load(file)
|
| 25 |
data.append(record)
|
| 26 |
with open(data_file, "w") as file:
|
| 27 |
json.dump(data, file)
|
| 28 |
-
return
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return pd.DataFrame(data)
|
| 36 |
-
except:
|
| 37 |
return pd.DataFrame(columns=["活動", "時間"])
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def login(username, password):
|
| 41 |
if username in users and users[username] == password:
|
| 42 |
return gr.update(visible=False), gr.update(visible=True), "登錄成功"
|
|
@@ -61,13 +84,34 @@ def main_interface():
|
|
| 61 |
main_section = gr.Column(visible=False)
|
| 62 |
with main_section:
|
| 63 |
gr.Markdown("### 🚀 點擊按鈕記錄活動")
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# 活動記錄表格
|
| 69 |
-
gr.Markdown("### 📊
|
| 70 |
-
data_table = gr.Dataframe(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# 登錄邏輯
|
| 73 |
login_btn.click(
|
|
@@ -76,13 +120,28 @@ def main_interface():
|
|
| 76 |
outputs=[login_section, main_section, login_msg]
|
| 77 |
)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
for
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
return app
|
| 88 |
|
|
|
|
| 2 |
import datetime
|
| 3 |
import pandas as pd
|
| 4 |
import json
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# 模擬用戶數據
|
| 8 |
+
users = {"admin": "password"}
|
| 9 |
+
|
| 10 |
+
# 數據存儲文件
|
| 11 |
data_file = "dog_activity_data.json"
|
|
|
|
| 12 |
|
| 13 |
# 初始化數據文件
|
| 14 |
def init_data_file():
|
| 15 |
+
if not os.path.exists(data_file):
|
|
|
|
|
|
|
|
|
|
| 16 |
with open(data_file, "w") as file:
|
| 17 |
json.dump([], file)
|
| 18 |
|
| 19 |
+
# 加載活動記錄
|
| 20 |
+
def load_data():
|
| 21 |
+
try:
|
| 22 |
+
with open(data_file, "r") as file:
|
| 23 |
+
return json.load(file)
|
| 24 |
+
except:
|
| 25 |
+
return []
|
| 26 |
+
|
| 27 |
+
# 保存活動記錄
|
| 28 |
def save_activity(activity):
|
| 29 |
timestamp = datetime.datetime.now()
|
| 30 |
record = {"活動": activity, "時間": timestamp.strftime("%Y-%m-%d %H:%M:%S")}
|
| 31 |
+
data = load_data()
|
|
|
|
| 32 |
data.append(record)
|
| 33 |
with open(data_file, "w") as file:
|
| 34 |
json.dump(data, file)
|
| 35 |
+
return update_activity_display()
|
| 36 |
|
| 37 |
+
# 更新顯示活動記錄 (僅顯示最近 5 條)
|
| 38 |
+
def update_activity_display(show_all=False):
|
| 39 |
+
data = load_data()
|
| 40 |
+
df = pd.DataFrame(data)
|
| 41 |
+
if df.empty:
|
|
|
|
|
|
|
| 42 |
return pd.DataFrame(columns=["活動", "時間"])
|
| 43 |
+
if not show_all:
|
| 44 |
+
df = df.tail(5)
|
| 45 |
+
return df
|
| 46 |
|
| 47 |
+
# 刪除記錄功能
|
| 48 |
+
def delete_activity(row_index):
|
| 49 |
+
data = load_data()
|
| 50 |
+
try:
|
| 51 |
+
row_index = int(row_index)
|
| 52 |
+
if 0 <= row_index < len(data):
|
| 53 |
+
del data[row_index]
|
| 54 |
+
with open(data_file, "w") as file:
|
| 55 |
+
json.dump(data, file)
|
| 56 |
+
return "記錄刪除成功", update_activity_display()
|
| 57 |
+
else:
|
| 58 |
+
return "錯誤: 行號超出範圍", update_activity_display()
|
| 59 |
+
except ValueError:
|
| 60 |
+
return "錯誤: 請輸入有效的數字行號", update_activity_display()
|
| 61 |
+
|
| 62 |
+
# 登錄功能
|
| 63 |
def login(username, password):
|
| 64 |
if username in users and users[username] == password:
|
| 65 |
return gr.update(visible=False), gr.update(visible=True), "登錄成功"
|
|
|
|
| 84 |
main_section = gr.Column(visible=False)
|
| 85 |
with main_section:
|
| 86 |
gr.Markdown("### 🚀 點擊按鈕記錄活動")
|
| 87 |
+
# 記錄活動按鈕 (帶顏色和 icon)
|
| 88 |
+
with gr.Row():
|
| 89 |
+
activity_buttons = {
|
| 90 |
+
"💩 拉屎": "#FF6347",
|
| 91 |
+
"💦 尿尿": "#4682B4",
|
| 92 |
+
"🍽️ 全吃了": "#32CD32",
|
| 93 |
+
"🍛 吃了一半": "#FFD700",
|
| 94 |
+
"🥄 沒怎麼吃": "#D3D3D3"
|
| 95 |
+
}
|
| 96 |
+
button_list = []
|
| 97 |
+
for name, color in activity_buttons.items():
|
| 98 |
+
button_list.append(
|
| 99 |
+
gr.Button(name, elem_id=name, interactive=True, color=color)
|
| 100 |
+
)
|
| 101 |
|
| 102 |
# 活動記錄表格
|
| 103 |
+
gr.Markdown("### 📊 最近活動記錄 (僅顯示最近5條)")
|
| 104 |
+
data_table = gr.Dataframe(update_activity_display(), interactive=False)
|
| 105 |
+
|
| 106 |
+
# 顯示更多按鈕
|
| 107 |
+
show_more_btn = gr.Button("顯示更多記錄")
|
| 108 |
+
show_less_btn = gr.Button("顯示最近5條", visible=False)
|
| 109 |
+
|
| 110 |
+
# 刪除記錄功能
|
| 111 |
+
gr.Markdown("### 🗑️ 刪除記錄")
|
| 112 |
+
row_to_delete = gr.Textbox(label="輸入要刪除的行號 (從 0 開始)")
|
| 113 |
+
delete_btn = gr.Button("刪除記錄")
|
| 114 |
+
delete_msg = gr.Textbox(label="刪除狀態", interactive=False)
|
| 115 |
|
| 116 |
# 登錄邏輯
|
| 117 |
login_btn.click(
|
|
|
|
| 120 |
outputs=[login_section, main_section, login_msg]
|
| 121 |
)
|
| 122 |
|
| 123 |
+
# 綁定活動按鈕邏輯
|
| 124 |
+
for button, activity in zip(button_list, activity_buttons.keys()):
|
| 125 |
+
button.click(save_activity, inputs=[], outputs=[data_table])
|
| 126 |
+
|
| 127 |
+
# 綁定顯示更多邏輯
|
| 128 |
+
show_more_btn.click(
|
| 129 |
+
lambda: (update_activity_display(show_all=True), gr.update(visible=False), gr.update(visible=True)),
|
| 130 |
+
inputs=[],
|
| 131 |
+
outputs=[data_table, show_more_btn, show_less_btn]
|
| 132 |
+
)
|
| 133 |
+
show_less_btn.click(
|
| 134 |
+
lambda: (update_activity_display(show_all=False), gr.update(visible=True), gr.update(visible=False)),
|
| 135 |
+
inputs=[],
|
| 136 |
+
outputs=[data_table, show_more_btn, show_less_btn]
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# 刪除記錄邏輯
|
| 140 |
+
delete_btn.click(
|
| 141 |
+
delete_activity,
|
| 142 |
+
inputs=[row_to_delete],
|
| 143 |
+
outputs=[delete_msg, data_table]
|
| 144 |
+
)
|
| 145 |
|
| 146 |
return app
|
| 147 |
|