xycold commited on
Commit
57e7263
·
verified ·
1 Parent(s): bbeb7d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -57
app.py CHANGED
@@ -1,66 +1,59 @@
 
1
  import pandas as pd
2
  import matplotlib.pyplot as plt
3
- import gradio as gr
4
- import tempfile
5
- import warnings
6
 
7
- warnings.filterwarnings(action='ignore', category=UserWarning)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # 设置中文字体和编码
10
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 选择合适的中文字体,这里使用宋体
11
- plt.rcParams['axes.unicode_minus'] = False # 设置正常显示字符
 
 
 
 
 
 
12
 
13
- def process_data(file_name, column1, column2, is_continuous, bins):
14
- # column1 = "身高"
15
- # column2 = "心血管疾病"
16
- # is_continuous = True
17
- # bins = 5
18
- # 读取数据
19
- df = pd.read_csv(file_name)
20
- data_x = df[column1]
21
- data_y = df[column2]
22
- # 自动判断column1的数据类型
23
- if is_continuous:
24
- # 如果是连续值,则进行分组
25
- data_x = pd.qcut(data_x, q=bins, duplicates='drop')
26
- else:
27
- # 如果是离散值,则直接使用
28
- pass
29
- # 统计每个身高分段中不同心血管疾病类别的数量
30
- counts = pd.crosstab(data_x, data_y)
31
- # 绘制分段柱形图
32
- counts.plot(kind='bar')
33
- # 设置 x 轴刻度标签横向显示
34
- plt.xticks(rotation=0)
35
- plt.xlabel(column1, fontsize=12)
36
- plt.ylabel(column2, fontsize=12)
37
- # plt.legend(['不患病', '患病'])
38
- plt.title(f'{column1}与{column2}的关系', fontsize=14)
39
- # plt.show()
40
 
41
- # 将图表保存为PNG图像文件
42
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp:
43
- temp_image_path = temp.name
44
- plt.savefig(temp_image_path)
 
 
 
 
 
45
 
46
- return df.head(), temp_image_path
 
 
 
 
47
 
48
- # 创建Gradio界面
49
- iface = gr.Interface(
50
- fn=process_data,
51
- inputs=[
52
- gr.File(label="上传数据表格"),
53
- gr.Textbox(label="指定第1个数据列名称"),
54
- gr.Textbox(label="指定第2个数据列名称"),
55
- gr.Checkbox(label="第1列数据是连续值类型", default=False),
56
- gr.Slider(3, 6, label="如果第1列是连续值,要分为几组"),
57
- ],
58
- outputs=[
59
- gr.Dataframe(type='pandas', label="数据表的前5行"),
60
- gr.Image(type='filepath', label="柱形图")
61
- ],
62
- title="数据分布可视化工具",
63
- description="上传数据表格,指定列名称,查看特定数据列分布的可视化结果。"
64
- )
65
 
66
- iface.launch(share=True)
 
1
+ import gradio as gr
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
 
 
 
4
 
5
+ def process_file(file):
6
+ # 读取CSV文件并创建DataFrame
7
+ df = pd.read_csv(file.name)
8
+ columns = df.columns.tolist()
9
+ print("文件已上传,表头为:", columns) # 调试信息
10
+ # 返回前5行数据,更新下拉列表选项,并使其他控件可见
11
+ return (df.head(),
12
+ gr.update(visible=True),
13
+ gr.update(choices=columns, visible=True),
14
+ gr.update(choices=columns, visible=True),
15
+ gr.update(visible=True))
16
+
17
+ def update_slider(choice):
18
+ print("选择框的值:", choice) # 调试信息
19
+ # 更新数轴控件的可见性
20
+ return gr.update(visible=choice == "是")
21
+
22
+ def generate_output(file, col1, col2, choice, number):
23
+ df = pd.read_csv(file.name)
24
+ filtered_data = df[[col1, col2]].dropna()
25
 
26
+ plt.figure(figsize=(10, 6))
27
+ plt.scatter(filtered_data[col1], filtered_data[col2])
28
+ plt.xlabel(col1)
29
+ plt.ylabel(col2)
30
+ plt.title(f'Scatter plot of {col1} vs {col2}')
31
+
32
+ image_path = 'output.png'
33
+ plt.savefig(image_path)
34
+ plt.close()
35
 
36
+ return filtered_data.head(), image_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ with gr.Blocks() as demo:
39
+ file_input = gr.File(label="上传CSV文件", file_types=["csv"])
40
+ df_display = gr.Dataframe(visible=False)
41
+ col1_dropdown = gr.Dropdown(label="选择列1", visible=False)
42
+ col2_dropdown = gr.Dropdown(label="选择列2", visible=False)
43
+ choice_radio = gr.Radio(["是", "否"], label="是否选择", visible=False)
44
+ slider = gr.Slider(minimum=2, maximum=7, step=1, label="选择数字", visible=False)
45
+ submit_button = gr.Button("提交")
46
+ output_image = gr.Image(visible=False)
47
 
48
+ # 文件上传后调用 process_file 函数
49
+ file_input.upload(process_file, inputs=file_input, outputs=[df_display, col1_dropdown, col2_dropdown, choice_radio])
50
+
51
+ # 选择框值改变时调用 update_slider 函数
52
+ choice_radio.change(update_slider, inputs=choice_radio, outputs=slider)
53
 
54
+ # 点击提交按钮时调用 generate_output 函数
55
+ submit_button.click(generate_output, inputs=[file_input, col1_dropdown, col2_dropdown, choice_radio, slider], outputs=[df_display, output_image])
56
+
57
+ demo.launch()
58
+
 
 
 
 
 
 
 
 
 
 
 
 
59