kedimestan commited on
Commit
b13bccc
·
verified ·
1 Parent(s): 3175c1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -35
app.py CHANGED
@@ -1,55 +1,77 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
 
5
- def update_dropdowns(file):
6
- if file is None:
7
  return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
8
- df = pd.read_csv(file)
9
- columns = list(df.columns)
10
- return gr.Dropdown(choices=columns), gr.Dropdown(choices=columns)
11
 
12
- def create_scatter_plot(file, x_col, y_col):
13
- if not file or not x_col or not y_col:
14
- return None
15
-
16
- df = pd.read_csv(file)
17
-
18
- plt.figure(figsize=(10, 6))
19
- plt.scatter(df[x_col], df[y_col])
20
- plt.title(f"{x_col} vs {y_col}")
21
- plt.xlabel(x_col)
22
- plt.ylabel(y_col)
23
- plt.grid(True)
24
 
25
- plt.savefig("scatter_plot.png")
26
- return "scatter_plot.png"
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  with gr.Blocks() as app:
29
- gr.Markdown("# CSV Veri Görselleştirme Aracı")
30
 
31
  with gr.Row():
32
- csv_file = gr.File(label="CSV Dosyası Yükle", type="filepath")
 
 
33
 
34
  with gr.Row():
35
- x_axis = gr.Dropdown(label="X Ekseni Parametresi", interactive=True)
36
- y_axis = gr.Dropdown(label="Y Ekseni Parametresi", interactive=True)
37
-
38
- plot_button = gr.Button("Grafiği Oluştur")
39
 
40
- with gr.Row():
41
- plot_output = gr.Image(label="Scatter Plot")
42
 
43
- csv_file.change(
 
44
  fn=update_dropdowns,
45
- inputs=csv_file,
46
  outputs=[x_axis, y_axis]
47
  )
48
-
49
- plot_button.click(
 
 
 
 
 
 
50
  fn=create_scatter_plot,
51
- inputs=[csv_file, x_axis, y_axis],
52
- outputs=plot_output
 
 
 
 
 
 
 
53
  )
54
 
55
- app.launch()
 
 
1
  import gradio as gr
2
  import pandas as pd
3
+ from vega_datasets import data
4
 
5
+ def update_dropdowns(csv_file):
6
+ if csv_file is None:
7
  return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
8
+ df = pd.read_csv(csv_file.name)
9
+ numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
10
+ return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols)
11
 
12
+ def plot_selection(selection, df):
13
+ if selection is None or df is None:
14
+ return pd.DataFrame()
15
+ return df.iloc[[i["index"] for i in selection]]
16
+
17
+ def create_scatter_plot(csv_file, x_col, y_col):
18
+ if csv_file is None or x_col is None or y_col is None:
19
+ return None, None
 
 
 
 
20
 
21
+ df = pd.read_csv(csv_file.name)
22
+ plot = gr.ScatterPlot(
23
+ value=df,
24
+ x=x_col,
25
+ y=y_col,
26
+ title="Custom CSV Scatter Plot",
27
+ x_title=x_col,
28
+ y_title=y_col,
29
+ tooltip=df.columns.tolist(),
30
+ color_legend_title="Values",
31
+ interactive=True,
32
+ brush_radius=5 # Lasso/polygon seçim için
33
+ )
34
+ return plot, df
35
 
36
  with gr.Blocks() as app:
37
+ gr.Markdown("## 📊 Custom CSV Scatter Plot with Selection")
38
 
39
  with gr.Row():
40
+ csv_upload = gr.UploadButton(label="Upload CSV", file_types=[".csv"])
41
+ x_axis = gr.Dropdown(label="X Axis Column")
42
+ y_axis = gr.Dropdown(label="Y Axis Column")
43
 
44
  with gr.Row():
45
+ plot = gr.ScatterPlot(interactive=True, brush_radius=5)
46
+ selected_data = gr.DataFrame(label="Selected Points")
 
 
47
 
48
+ df_store = gr.State()
 
49
 
50
+ # CSV yüklendiğinde dropdown'ları güncelle
51
+ csv_upload.upload(
52
  fn=update_dropdowns,
53
+ inputs=csv_upload,
54
  outputs=[x_axis, y_axis]
55
  )
56
+
57
+ # Eksenler seçildiğinde grafiği oluştur
58
+ x_axis.change(
59
+ fn=create_scatter_plot,
60
+ inputs=[csv_upload, x_axis, y_axis],
61
+ outputs=[plot, df_store]
62
+ )
63
+ y_axis.change(
64
  fn=create_scatter_plot,
65
+ inputs=[csv_upload, x_axis, y_axis],
66
+ outputs=[plot, df_store]
67
+ )
68
+
69
+ # Seçim yapıldığında verileri güncelle
70
+ plot.select(
71
+ fn=plot_selection,
72
+ inputs=df_store,
73
+ outputs=selected_data
74
  )
75
 
76
+ if __name__ == "__main__":
77
+ app.launch()