kedimestan commited on
Commit
da71915
·
verified ·
1 Parent(s): e94f3be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -73
app.py CHANGED
@@ -1,93 +1,57 @@
1
  import gradio as gr
2
  import pandas as pd
3
- from shapely.geometry import Point, Polygon
4
 
5
- def load_csv(file):
6
- return pd.read_csv(file.name)
7
-
8
- def update_dropdowns(df):
9
- if df is None or df.empty:
10
  return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
11
- columns = df.columns.tolist() # Index'i listeye çevir
 
12
  return gr.Dropdown(choices=columns), gr.Dropdown(choices=columns)
13
 
14
- def create_plot(df, x_col, y_col):
15
- if df is None or x_col not in df.columns or y_col not in df.columns:
16
  return None
17
- return {
18
- "x": df[x_col].tolist(),
19
- "y": df[y_col].tolist(),
20
- "color": "blue",
21
- "tooltip": df.columns.tolist(),
22
- "x_title": x_col,
23
- "y_title": y_col
24
- }
25
-
26
- def filter_points(event: gr.SelectData, df, x_col, y_col):
27
- if df is None or x_col not in df.columns or y_col not in df.columns:
28
- return pd.DataFrame()
29
-
30
- if not hasattr(filter_points, "points"):
31
- filter_points.points = []
32
 
33
- filter_points.points.append((event.x, event.y))
34
 
35
- if len(filter_points.points) < 3:
36
- return pd.DataFrame()
 
 
 
 
37
 
38
- try:
39
- polygon = Polygon(filter_points.points)
40
- mask = df[[x_col, y_col]].apply(
41
- lambda row: polygon.contains(Point(row[x_col], row[y_col])),
42
- axis=1
43
- )
44
- return df[mask]
45
- finally:
46
- filter_points.points = []
47
 
48
- with gr.Blocks() as demo:
49
- gr.Markdown("## 🎯 Çalışan Son Sürüm")
50
-
51
- df_state = gr.State()
52
 
53
  with gr.Row():
54
- csv_upload = gr.File(label="1. CSV Yükle", file_types=[".csv"])
55
- x_col = gr.Dropdown(label="2. X Sütunu Seç")
56
- y_col = gr.Dropdown(label="3. Y Sütunu Seç")
57
-
58
- plot = gr.ScatterPlot(
59
- label="4. Lasso Tool ile Alan Seç",
60
- show_label=True,
61
- interactive=True
62
- )
63
 
64
- results = gr.DataFrame(label="Seçilen Veriler")
 
 
65
 
66
- csv_upload.upload(
67
- load_csv,
68
- inputs=csv_upload,
69
- outputs=df_state
70
- ).then(
71
- update_dropdowns,
72
- inputs=df_state,
73
- outputs=[x_col, y_col]
74
- )
75
 
76
- x_col.change(
77
- create_plot,
78
- inputs=[df_state, x_col, y_col],
79
- outputs=plot
80
- )
81
- y_col.change(
82
- create_plot,
83
- inputs=[df_state, x_col, y_col],
84
- outputs=plot
85
  )
86
 
87
- plot.select(
88
- filter_points,
89
- inputs=[df_state, x_col, y_col],
90
- outputs=results
 
91
  )
92
 
93
- demo.launch()
 
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.name)
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 file is None or not x_col or not y_col:
14
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ df = pd.read_csv(file.name)
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="file")
 
 
 
 
 
 
 
 
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
+ # Dropdown'ları güncelleme
44
+ csv_file.change(
45
+ fn=update_dropdowns,
46
+ inputs=csv_file,
47
+ outputs=[x_axis, y_axis]
 
48
  )
49
 
50
+ # Plot oluşturma
51
+ plot_button.click(
52
+ fn=create_scatter_plot,
53
+ inputs=[csv_file, x_axis, y_axis],
54
+ outputs=plot_output
55
  )
56
 
57
+ app.launch()