Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,70 +3,76 @@ import json
|
|
3 |
import os
|
4 |
from datetime import datetime
|
5 |
|
6 |
-
# JSON文件路径
|
7 |
-
|
8 |
|
9 |
-
# 初始化JSON
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
-
# 读取JSON
|
15 |
def read_json():
|
16 |
-
with open(
|
17 |
data = json.load(f)
|
18 |
-
return data
|
19 |
|
20 |
-
#
|
21 |
-
def
|
22 |
-
with open(
|
23 |
-
json.dump(
|
24 |
|
25 |
-
#
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
records.append({"activity": activity, "time": time_stamp})
|
33 |
-
update_json(records)
|
34 |
-
|
35 |
-
return records
|
36 |
|
37 |
-
#
|
38 |
def delete_record(index):
|
39 |
-
|
40 |
-
if 0 <= index < len(
|
41 |
-
|
42 |
-
|
43 |
-
return
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# 设置按钮
|
51 |
-
with gr.Row():
|
52 |
-
gr.Button("拉屎").click(record_activity, inputs="拉屎", outputs=output)
|
53 |
-
gr.Button("尿尿").click(record_activity, inputs="尿尿", outputs=output)
|
54 |
-
gr.Button("全吃了").click(record_activity, inputs="全吃了", outputs=output)
|
55 |
-
gr.Button("吃了一半").click(record_activity, inputs="吃了一半", outputs=output)
|
56 |
-
gr.Button("没吃").click(record_activity, inputs="没吃", outputs=output)
|
57 |
-
|
58 |
-
# 删除记录按钮
|
59 |
-
delete_index = gr.Number(label="删除记录索引", interactive=True)
|
60 |
-
delete_button = gr.Button("删除记录")
|
61 |
-
delete_button.click(delete_record, inputs=delete_index, outputs=output)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
return "\n".join([f"{record['time']} - {record['activity']}" for record in records])
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
from datetime import datetime
|
5 |
|
6 |
+
# 定义JSON文件路径
|
7 |
+
JSON_FILE = "1.json"
|
8 |
|
9 |
+
# 初始化JSON文件
|
10 |
+
def initialize_json():
|
11 |
+
if not os.path.exists(JSON_FILE):
|
12 |
+
with open(JSON_FILE, 'w') as f:
|
13 |
+
json.dump([], f)
|
14 |
|
15 |
+
# 读取JSON文件
|
16 |
def read_json():
|
17 |
+
with open(JSON_FILE, 'r') as f:
|
18 |
data = json.load(f)
|
19 |
+
return data
|
20 |
|
21 |
+
# 写入JSON文件
|
22 |
+
def write_json(data):
|
23 |
+
with open(JSON_FILE, 'w') as f:
|
24 |
+
json.dump(data, f, indent=4)
|
25 |
|
26 |
+
# 记录事件
|
27 |
+
def record_event(event_type):
|
28 |
+
data = read_json()
|
29 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
30 |
+
data.append({"timestamp": timestamp, "event": event_type})
|
31 |
+
write_json(data)
|
32 |
+
return data
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# 删除记录
|
35 |
def delete_record(index):
|
36 |
+
data = read_json()
|
37 |
+
if 0 <= index < len(data):
|
38 |
+
data.pop(index)
|
39 |
+
write_json(data)
|
40 |
+
return data
|
41 |
|
42 |
+
# 显示记录
|
43 |
+
def display_records():
|
44 |
+
data = read_json()
|
45 |
+
return "\n".join([f"{i}: {item['timestamp']} - {item['event']}" for i, item in enumerate(data)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Gradio界面
|
48 |
+
def main():
|
49 |
+
initialize_json()
|
|
|
50 |
|
51 |
+
with gr.Blocks() as demo:
|
52 |
+
gr.Markdown("## 狗狗日常记录")
|
53 |
+
|
54 |
+
with gr.Row():
|
55 |
+
poop_btn = gr.Button("拉屎", variant="primary")
|
56 |
+
pee_btn = gr.Button("尿尿", variant="secondary")
|
57 |
+
eat_all_btn = gr.Button("全吃了", variant="success")
|
58 |
+
eat_half_btn = gr.Button("吃了一半", variant="warning")
|
59 |
+
eat_none_btn = gr.Button("没吃", variant="danger")
|
60 |
+
|
61 |
+
output = gr.Textbox(label="记录", interactive=False)
|
62 |
+
delete_index = gr.Number(label="删除记录的索引", precision=0)
|
63 |
+
delete_btn = gr.Button("删除记录")
|
64 |
|
65 |
+
poop_btn.click(lambda: record_event("拉屎"), None, output, queue=False)
|
66 |
+
pee_btn.click(lambda: record_event("尿尿"), None, output, queue=False)
|
67 |
+
eat_all_btn.click(lambda: record_event("全吃了"), None, output, queue=False)
|
68 |
+
eat_half_btn.click(lambda: record_event("吃了一半"), None, output, queue=False)
|
69 |
+
eat_none_btn.click(lambda: record_event("没吃"), None, output, queue=False)
|
70 |
+
|
71 |
+
delete_btn.click(lambda idx: delete_record(int(idx)), delete_index, output, queue=False)
|
72 |
+
|
73 |
+
output.update(display_records())
|
74 |
+
|
75 |
+
demo.launch()
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
main()
|