xycold commited on
Commit
6b4bacb
1 Parent(s): 08cb4ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -23
app.py CHANGED
@@ -2,59 +2,95 @@ 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(choices=columns, visible=True),
13
- gr.update(choices=columns, visible=True),
14
  gr.update(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
  with gr.Row():
40
  with gr.Column():
41
- file_input = gr.File(label="上传CSV文件", file_types=["csv"])
42
  col1_dropdown = gr.Dropdown(label="选择列1", visible=False)
43
  col2_dropdown = gr.Dropdown(label="选择列2", visible=False)
44
  choice_radio = gr.Radio(["是", "否"], label="是否选择", visible=False)
45
- slider = gr.Slider(minimum=2, maximum=7, step=1, label="选择数字", visible=False)
46
  submit_button = gr.Button("提交")
47
  with gr.Column():
48
  df_display = gr.Dataframe(visible=False)
49
  output_image = gr.Image(visible=False)
50
 
51
  # 文件上传后调用 process_file 函数
52
- file_input.upload(process_file, inputs=file_input, outputs=[col1_dropdown, col2_dropdown, choice_radio, df_display])
53
 
54
  # 选择框值改变时调用 update_slider 函数
55
  choice_radio.change(update_slider, inputs=choice_radio, outputs=slider)
56
 
57
  # 点击提交按钮时调用 generate_output 函数
58
- submit_button.click(generate_output, inputs=[file_input, col1_dropdown, col2_dropdown, choice_radio, slider], outputs=[df_display, output_image])
59
 
60
- demo.launch()
 
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
 
5
+ import pandas as pd
6
+ import matplotlib.pyplot as plt
7
+ import gradio as gr
8
+ import tempfile
9
+ import warnings
10
+
11
+ warnings.filterwarnings(action='ignore', category=UserWarning)
12
+
13
+ # 设置中文字体和编码
14
+ plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # 选择合适的中文字体,这里使用宋体
15
+ plt.rcParams['axes.unicode_minus'] = False # 设置正常显示字符
16
+
17
  def process_file(file):
18
+ # 读取CSV或Excel文件并创建DataFrame
19
+ if file.name.endswith('.csv'):
20
+ df = pd.read_csv(file.name)
21
+ elif file.name.endswith('.xlsx') or file.name.endswith('.xls'):
22
+ df = pd.read_excel(file.name)
23
+ else:
24
+ return "不支持的数据文件格式。"
25
+
26
  columns = df.columns.tolist()
27
+ feature_columns = columns[:-1]
28
+ last_column = [columns[-1]]
29
  # 返回前5行数据,更新下拉列表选项,并使其他控件可见
30
+ return (gr.update(choices=feature_columns, value=feature_columns[0], visible=True),
31
+ gr.update(choices=last_column, value=last_column[-1], visible=True),
32
+ gr.update(visible=True),
33
  gr.update(visible=True),
34
+ df.head()
35
+ )
36
 
37
  def update_slider(choice):
 
38
  # 更新数轴控件的可见性
39
  return gr.update(visible=choice == "是")
40
 
41
+ def generate_output(file, column1, column2, choice, bins):
42
  df = pd.read_csv(file.name)
43
+ data_x = df[column1]
44
+ data_y = df[column2]
45
 
46
+ # 自动判断column1的数据类型
47
+ if choice == "是":
48
+ # 如果是连续值,则进行分组
49
+ data_x = pd.qcut(data_x, q=bins, duplicates='drop')
50
+ else:
51
+ # 如果是离散值,则直接使用
52
+ pass
53
 
54
+ # 统计每个身高分段中不同心血管疾病类别的数量
55
+ counts = pd.crosstab(data_x, data_y)
56
+ # 绘制分段柱形图
57
+ counts.plot(kind='bar')
58
+ # 设置画布大小
59
+ plt.figure(figsize=(bins*2, 6))
60
+ # 设置 x 轴刻度标签横向显示
61
+ plt.xticks(rotation=0)
62
+ plt.xlabel(column1, fontsize=12)
63
+ plt.ylabel(column2, fontsize=12)
64
+ # plt.legend(['不患病', '患病'])
65
+ plt.title(f'{column1}与{column2}的关系', fontsize=14)
66
+ # plt.show()
67
+
68
  image_path = 'output.png'
69
  plt.savefig(image_path)
70
+ # plt.close()
 
 
71
 
72
+ return df.head(), gr.update(visible=True), image_path, gr.update(visible=True)
73
+
74
  with gr.Blocks() as demo:
75
  with gr.Row():
76
  with gr.Column():
77
+ file_input = gr.File(label="上传表格文件(支持CSV、XLS、XLSX等格式", file_types=["csv", "xls", "xlsx"])
78
  col1_dropdown = gr.Dropdown(label="选择列1", visible=False)
79
  col2_dropdown = gr.Dropdown(label="选择列2", visible=False)
80
  choice_radio = gr.Radio(["是", "否"], label="是否选择", visible=False)
81
+ slider = gr.Slider(minimum=3, maximum=7, step=1, label="选择数字", visible=False, value=4)
82
  submit_button = gr.Button("提交")
83
  with gr.Column():
84
  df_display = gr.Dataframe(visible=False)
85
  output_image = gr.Image(visible=False)
86
 
87
  # 文件上传后调用 process_file 函数
88
+ file_input.upload(process_file, inputs=file_input, outputs=[col1_dropdown, col2_dropdown, choice_radio, df_display, df_display])
89
 
90
  # 选择框值改变时调用 update_slider 函数
91
  choice_radio.change(update_slider, inputs=choice_radio, outputs=slider)
92
 
93
  # 点击提交按钮时调用 generate_output 函数
94
+ submit_button.click(generate_output, inputs=[file_input, col1_dropdown, col2_dropdown, choice_radio, slider], outputs=[df_display, df_display, output_image, output_image])
95
 
96
+ demo.launch(share=True)