Spaces:
Sleeping
Sleeping
init!
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import datetime
|
3 |
+
import pandas as pd
|
4 |
+
import json
|
5 |
+
|
6 |
+
# 模擬數據存儲(可以換成 Firebase 或雲端數據庫)
|
7 |
+
data_file = "dog_activity_data.json"
|
8 |
+
users = {"admin": "password"} # 模擬用戶數據
|
9 |
+
|
10 |
+
# 初始化數據文件
|
11 |
+
def init_data_file():
|
12 |
+
try:
|
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": activity, "timestamp": timestamp.strftime("%Y-%m-%d %H:%M:%S")}
|
23 |
+
with open(data_file, "r") as file:
|
24 |
+
data = json.load(file)
|
25 |
+
data.append(record)
|
26 |
+
with open(data_file, "w") as file:
|
27 |
+
json.dump(data, file)
|
28 |
+
return f"記錄成功:{activity} - {record['timestamp']}"
|
29 |
+
|
30 |
+
# 獲取活動數據並展示為表格
|
31 |
+
def get_activity_data():
|
32 |
+
with open(data_file, "r") as file:
|
33 |
+
data = json.load(file)
|
34 |
+
df = pd.DataFrame(data)
|
35 |
+
return df if not df.empty else "目前沒有數據。"
|
36 |
+
|
37 |
+
# 驗證用戶登錄
|
38 |
+
def login(username, password):
|
39 |
+
if username in users and users[username] == password:
|
40 |
+
return True, "登錄成功"
|
41 |
+
else:
|
42 |
+
return False, "用戶名或密碼錯誤"
|
43 |
+
|
44 |
+
# 主界面
|
45 |
+
def main_interface():
|
46 |
+
with gr.Blocks() as app:
|
47 |
+
# 登錄界面
|
48 |
+
with gr.Row():
|
49 |
+
gr.Markdown("## 狗狗活動記錄器 - 登錄")
|
50 |
+
with gr.Row():
|
51 |
+
username = gr.Textbox(label="用戶名", placeholder="輸入用戶名")
|
52 |
+
password = gr.Textbox(label="密碼", type="password", placeholder="輸入密碼")
|
53 |
+
login_btn = gr.Button("登錄")
|
54 |
+
login_msg = gr.Textbox(label="登錄消息", interactive=False)
|
55 |
+
|
56 |
+
# 隱藏的主界面
|
57 |
+
main_content = gr.Group(visible=False)
|
58 |
+
with main_content:
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column():
|
61 |
+
gr.Markdown("### 輸入活動類型")
|
62 |
+
activity_input = gr.Dropdown(
|
63 |
+
choices=["拉屎", "尿尿", "全吃了", "吃了一半", "沒怎麼吃"],
|
64 |
+
label="活動"
|
65 |
+
)
|
66 |
+
activity_btn = gr.Button("記錄活動")
|
67 |
+
output_msg = gr.Textbox(label="系統消息", interactive=False)
|
68 |
+
with gr.Column():
|
69 |
+
gr.Markdown("### 活動記錄")
|
70 |
+
data_table = gr.Dataframe(headers=["活動", "時間"], datatype=["str", "str"], interactive=False)
|
71 |
+
refresh_btn = gr.Button("刷新數據")
|
72 |
+
|
73 |
+
# 功能邏輯
|
74 |
+
def handle_login(username, password):
|
75 |
+
success, msg = login(username, password)
|
76 |
+
return gr.update(visible=success), gr.update(value=msg)
|
77 |
+
|
78 |
+
login_btn.click(handle_login, inputs=[username, password], outputs=[main_content, login_msg])
|
79 |
+
activity_btn.click(lambda x: save_activity(x), inputs=activity_input, outputs=output_msg)
|
80 |
+
refresh_btn.click(get_activity_data, outputs=data_table)
|
81 |
+
|
82 |
+
return app
|
83 |
+
|
84 |
+
# 初始化數據文件
|
85 |
+
init_data_file()
|
86 |
+
|
87 |
+
# 運行應用
|
88 |
+
main_interface().launch()
|