ake178178 commited on
Commit
a923270
·
verified ·
1 Parent(s): 16d00a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -55
app.py CHANGED
@@ -3,70 +3,76 @@ import json
3
  import os
4
  from datetime import datetime
5
 
6
- # JSON文件路径
7
- json_file_path = "1.json"
8
 
9
- # 初始化JSON文件,如果文件不存在
10
- if not os.path.exists(json_file_path):
11
- with open(json_file_path, "w") as f:
12
- json.dump({"records": []}, f)
 
13
 
14
- # 读取JSON文件内容
15
  def read_json():
16
- with open(json_file_path, "r") as f:
17
  data = json.load(f)
18
- return data["records"]
19
 
20
- # 更新JSON文件
21
- def update_json(records):
22
- with open(json_file_path, "w") as f:
23
- json.dump({"records": records}, f)
24
 
25
- # 按钮点击后记录数据
26
- def record_activity(activity):
27
- # 获取当前时间
28
- time_stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
29
- records = read_json()
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
- records = read_json()
40
- if 0 <= index < len(records):
41
- del records[index]
42
- update_json(records)
43
- return records
44
 
45
- # 设置Gradio界面
46
- with gr.Blocks() as demo:
47
- # 显示当前记录的表格
48
- output = gr.Textbox(label="Current Records", interactive=False)
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
- def update_output():
65
- records = read_json()
66
- return "\n".join([f"{record['time']} - {record['activity']}" for record in records])
67
 
68
- # 设置定时器自动更新显示
69
- output.update(update_output)
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- # 启动Gradio应用
72
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
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()