Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,81 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return f"记录 {time_to_delete} 已删除!"
|
34 |
|
35 |
-
#
|
36 |
-
def
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
#
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
#
|
|
|
|
|
|
|
|
|
|
|
44 |
with gr.Blocks() as demo:
|
45 |
-
gr.Markdown("
|
46 |
-
|
|
|
47 |
with gr.Row():
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
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()
|