ake178178 commited on
Commit
2927440
·
verified ·
1 Parent(s): 20d6d5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -49
app.py CHANGED
@@ -1,62 +1,81 @@
1
  import gradio as gr
2
- import pandas as pd
3
- from datetime import datetime
4
-
5
- # 初始化CSV文件路径
6
- csv_file = "dog_records.csv"
7
-
8
- # 如果CSV文件不存在,则创建文件并添加列名
9
- def init_csv():
10
- try:
11
- df = pd.read_csv(csv_file)
12
- except FileNotFoundError:
13
- df = pd.DataFrame(columns=["time", "action", "details"])
14
- df.to_csv(csv_file, index=False)
15
-
16
- # 记录数据到CSV
17
- def record_data(action, details):
18
- time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
19
- new_record = pd.DataFrame([[time, action, details]], columns=["time", "action", "details"])
20
-
21
- # 读取现有的CSV并追加新记录
22
- df = pd.read_csv(csv_file)
23
- df = pd.concat([df, new_record], ignore_index=True)
24
- df.to_csv(csv_file, index=False)
25
 
26
- return "记录已保存!"
 
 
 
 
27
 
28
- # 删除错误记录
29
- def delete_record(time_to_delete):
30
- df = pd.read_csv(csv_file)
31
- df = df[df['time'] != time_to_delete] # 删除匹配的记录
32
- df.to_csv(csv_file, index=False)
33
- return f"记录 {time_to_delete} 已删除!"
34
 
35
- # 查看最近的记录
36
- def show_records():
37
- df = pd.read_csv(csv_file)
38
- return df.tail(10) # 显示最近的10条记录
 
 
 
 
 
 
 
 
 
39
 
40
- # 初始化CSV文件
41
- init_csv()
 
 
 
 
 
42
 
43
- # 创建Gradio界面
 
 
 
 
 
44
  with gr.Blocks() as demo:
45
- gr.Markdown("### 记录狗狗的活动时间(拉屎、尿尿、吃东西)")
46
-
 
47
  with gr.Row():
48
- action_input = gr.Dropdown(choices=["拉屎", "尿尿", "吃东西"], label="选择活动")
49
- details_input = gr.Dropdown(choices=["全吃了", "吃了一半", "没吃"], label="吃东西情况")
 
 
 
 
 
 
 
 
50
 
51
- record_button = gr.Button("记录")
52
- delete_button = gr.Button("删除记录")
53
- show_button = gr.Button("查看最近记录")
 
54
 
55
- output_text = gr.Textbox(label="反馈")
56
- output_table = gr.DataFrame()
 
 
 
 
 
 
 
57
 
58
- record_button.click(record_data, inputs=[action_input, details_input], outputs=output_text)
59
- delete_button.click(delete_record, inputs=gr.Textbox(), outputs=output_text)
60
- show_button.click(show_records, outputs=output_table)
61
 
 
62
  demo.launch()
 
1
  import gradio as gr
2
+ import json
3
+ import os
4
+ import datetime
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # 确保 1.json 文件存在,如果不存在则创建
7
+ json_file_path = '1.json'
8
+ if not os.path.exists(json_file_path):
9
+ with open(json_file_path, 'w') as f:
10
+ json.dump([], f)
11
 
12
+ # 加载现有记录
13
+ def load_records():
14
+ with open(json_file_path, 'r') as f:
15
+ records = json.load(f)
16
+ return records
 
17
 
18
+ # 更新记录到文件
19
+ def save_records(records):
20
+ with open(json_file_path, 'w') as f:
21
+ json.dump(records, f)
22
+
23
+ # 记录操作
24
+ def record_action(action):
25
+ records = load_records()
26
+ timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
27
+ record = {'action': action, 'timestamp': timestamp}
28
+ records.append(record)
29
+ save_records(records)
30
+ return records
31
 
32
+ # 删除记录
33
+ def delete_record(index):
34
+ records = load_records()
35
+ if 0 <= index < len(records):
36
+ del records[index]
37
+ save_records(records)
38
+ return records
39
 
40
+ # 显示当前记录
41
+ def show_records():
42
+ records = load_records()
43
+ return '\n'.join([f"{rec['timestamp']} - {rec['action']}" for rec in records])
44
+
45
+ # Gradio UI
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("## 狗狗记录应用")
48
+
49
+ # 按钮定义
50
  with gr.Row():
51
+ record_buttons = [
52
+ gr.Button(value="拉屎", elem_id="defecation", color="brown"),
53
+ gr.Button(value="尿尿", elem_id="urination", color="blue"),
54
+ gr.Button(value="吃东西全吃了", elem_id="eaten_all", color="green"),
55
+ gr.Button(value="吃了一半", elem_id="eaten_half", color="yellow"),
56
+ gr.Button(value="没吃", elem_id="eaten_none", color="red")
57
+ ]
58
+
59
+ # 显示当前记录的文本框
60
+ output = gr.Textbox(label="记录", interactive=False, lines=10)
61
 
62
+ # 删除记录输入框和按钮
63
+ with gr.Row():
64
+ delete_index_input = gr.Number(label="删除记录索引(从0开始)", min=0)
65
+ delete_button = gr.Button("删除记录")
66
 
67
+ # 显示记录
68
+ show_button = gr.Button("显示记录")
69
+
70
+ # 动作按钮事件处理
71
+ for button in record_buttons:
72
+ button.click(record_action, inputs=[button], outputs=[output])
73
+
74
+ # 删除记录处理
75
+ delete_button.click(delete_record, inputs=[delete_index_input], outputs=[output])
76
 
77
+ # 显示记录按钮事件
78
+ show_button.click(show_records, outputs=[output])
 
79
 
80
+ # 启动 Gradio 应用
81
  demo.launch()