kedimestan commited on
Commit
a94c46b
·
verified ·
1 Parent(s): b1a8097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -35
app.py CHANGED
@@ -10,59 +10,59 @@ def update_dropdowns(df):
10
  return gr.Dropdown(choices=df.columns.tolist()), gr.Dropdown(choices=df.columns.tolist())
11
 
12
  def create_plot(df, x_col, y_col, polygon_points=None):
13
- if df is None or x_col is None or y_col is None:
14
- return None
15
-
16
  fig = go.Figure()
17
 
18
- # Ana scatter plot
19
- fig.add_trace(go.Scatter(
20
- x=df[x_col],
21
- y=df[y_col],
22
- mode='markers',
23
- name='Data Points',
24
- marker=dict(color='blue')
25
- ))
 
26
 
27
  # Polygon çizgileri
28
- if polygon_points and len(polygon_points) >= 2:
29
  fig.add_trace(go.Scatter(
30
  x=[p[0] for p in polygon_points],
31
  y=[p[1] for p in polygon_points],
32
  mode='lines+markers',
33
  line=dict(color='red', width=2),
34
- marker=dict(color='red', size=8),
35
  name='Polygon'
36
  ))
37
 
38
  fig.update_layout(
39
- title=f"{x_col} vs {y_col}",
40
- dragmode='drawclosedpath',
41
- showlegend=False
42
  )
43
-
44
  return fig
45
 
46
- def handle_click(click_data, df, x_col, y_col, polygon_points):
47
- if not click_data or not df.empty:
48
- return [], pd.DataFrame()
49
 
50
- # Tıklanan noktayı al
51
- point = (click_data['points'][0]['x'], click_data['points'][0]['y'])
52
- new_points = polygon_points + [point]
53
 
54
- # Polygon tamamlandıysa içinde kalan noktaları bul
55
- if len(new_points) >= 3 and new_points[0] == new_points[-1]:
56
- polygon = Polygon(new_points)
57
- mask = df.apply(lambda row: polygon.contains(Point(row[x_col], row[y_col])), axis=1)
58
- return [], df[mask]
 
 
 
59
 
60
- return new_points, pd.DataFrame()
61
 
62
  with gr.Blocks() as demo:
63
- gr.Markdown("## Interactive Polygon Selection Tool")
64
 
65
- df_state = gr.State(pd.DataFrame())
66
  polygon_state = gr.State([])
67
 
68
  with gr.Row():
@@ -70,9 +70,10 @@ with gr.Blocks() as demo:
70
  x_col = gr.Dropdown(label="X Sütunu")
71
  y_col = gr.Dropdown(label="Y Sütunu")
72
 
73
- plot = gr.Plot(label="Scatter Plot ve Polygon")
74
- results = gr.DataFrame(label="Seçilen Veriler")
75
 
 
76
  csv_upload.upload(
77
  load_csv,
78
  inputs=csv_upload,
@@ -83,6 +84,7 @@ with gr.Blocks() as demo:
83
  outputs=[x_col, y_col]
84
  )
85
 
 
86
  x_col.change(
87
  create_plot,
88
  inputs=[df_state, x_col, y_col, polygon_state],
@@ -95,10 +97,15 @@ with gr.Blocks() as demo:
95
  outputs=plot
96
  )
97
 
98
- plot.select(
99
- handle_click,
 
100
  inputs=[df_state, x_col, y_col, polygon_state],
101
  outputs=[polygon_state, results]
 
 
 
 
102
  )
103
 
104
  demo.launch()
 
10
  return gr.Dropdown(choices=df.columns.tolist()), gr.Dropdown(choices=df.columns.tolist())
11
 
12
  def create_plot(df, x_col, y_col, polygon_points=None):
 
 
 
13
  fig = go.Figure()
14
 
15
+ # Ana veri noktaları
16
+ if df is not None and x_col and y_col:
17
+ fig.add_trace(go.Scatter(
18
+ x=df[x_col],
19
+ y=df[y_col],
20
+ mode='markers',
21
+ marker=dict(color='blue', size=8),
22
+ name='Veri Noktaları'
23
+ ))
24
 
25
  # Polygon çizgileri
26
+ if polygon_points and len(polygon_points) > 0:
27
  fig.add_trace(go.Scatter(
28
  x=[p[0] for p in polygon_points],
29
  y=[p[1] for p in polygon_points],
30
  mode='lines+markers',
31
  line=dict(color='red', width=2),
32
+ marker=dict(color='red', size=10),
33
  name='Polygon'
34
  ))
35
 
36
  fig.update_layout(
37
+ title=f"{x_col} vs {y_col}" if x_col and y_col else "Veri Görselleştirme",
38
+ dragmode='drawpoint',
39
+ clickmode='event+select'
40
  )
 
41
  return fig
42
 
43
+ def handle_plot_click(click_data, df, x_col, y_col, polygon_points):
44
+ if not click_data or not x_col or not y_col:
45
+ return polygon_points, pd.DataFrame()
46
 
47
+ # Tıklanan noktanın koordinatlarını al
48
+ new_point = (click_data['points'][0]['x'], click_data['points'][0]['y'])
49
+ updated_points = polygon_points + [new_point]
50
 
51
+ # Polygon'u kontrol et ve noktaları filtrele
52
+ if len(updated_points) >= 3:
53
+ try:
54
+ poly = Polygon(updated_points)
55
+ mask = df.apply(lambda row: poly.contains(Point(row[x_col], row[y_col])), axis=1)
56
+ return updated_points, df[mask]
57
+ except:
58
+ return updated_points, pd.DataFrame()
59
 
60
+ return updated_points, pd.DataFrame()
61
 
62
  with gr.Blocks() as demo:
63
+ gr.Markdown("## Interaktif Polygon Seçim Aracı")
64
 
65
+ df_state = gr.State()
66
  polygon_state = gr.State([])
67
 
68
  with gr.Row():
 
70
  x_col = gr.Dropdown(label="X Sütunu")
71
  y_col = gr.Dropdown(label="Y Sütunu")
72
 
73
+ plot = gr.Plot(label="Veri Görselleştirme")
74
+ results = gr.DataFrame(label="Seçilen Veriler", interactive=False)
75
 
76
+ # CSV yükleme ve dropdown güncelleme
77
  csv_upload.upload(
78
  load_csv,
79
  inputs=csv_upload,
 
84
  outputs=[x_col, y_col]
85
  )
86
 
87
+ # Plot güncelleme
88
  x_col.change(
89
  create_plot,
90
  inputs=[df_state, x_col, y_col, polygon_state],
 
97
  outputs=plot
98
  )
99
 
100
+ # Tıklama olayı işleyicisi
101
+ plot.click(
102
+ handle_plot_click,
103
  inputs=[df_state, x_col, y_col, polygon_state],
104
  outputs=[polygon_state, results]
105
+ ).then(
106
+ create_plot,
107
+ inputs=[df_state, x_col, y_col, polygon_state],
108
+ outputs=plot
109
  )
110
 
111
  demo.launch()