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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -65
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import plotly.graph_objects as go
4
  from shapely.geometry import Point, Polygon
5
 
6
  def load_csv(file):
@@ -9,71 +8,53 @@ def load_csv(file):
9
  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
- 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():
69
  csv_upload = gr.File(label="CSV Yükle", file_types=[".csv"])
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,28 +65,24 @@ with gr.Blocks() as demo:
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],
91
  outputs=plot
92
  )
93
 
94
  y_col.change(
95
- create_plot,
96
- 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()
 
1
  import gradio as gr
2
  import pandas as pd
 
3
  from shapely.geometry import Point, Polygon
4
 
5
  def load_csv(file):
 
8
  def update_dropdowns(df):
9
  return gr.Dropdown(choices=df.columns.tolist()), gr.Dropdown(choices=df.columns.tolist())
10
 
11
+ def create_scatter(df, x_col, y_col):
12
+ if df is None or x_col is None or y_col is None:
13
+ return None
14
+ return gr.ScatterPlot(
15
+ value=df[[x_col, y_col]],
16
+ x=x_col,
17
+ y=y_col,
18
+ title=f"{x_col} vs {y_col}",
19
+ interactive=True,
20
+ tooltip=list(df.columns),
21
+ color="blue"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  )
 
23
 
24
+ def filter_points(selected_data, df, x_col, y_col):
25
+ if not selected_data or not df.empty:
26
+ return pd.DataFrame()
27
 
28
+ # Seçilen polygon noktalarını al
29
+ points = selected_data["points"]
30
+ if len(points) < 3:
31
+ return pd.DataFrame()
32
 
33
+ # Polygon oluştur
34
+ polygon = Polygon([(p["x"], p["y"]) for p in points])
 
 
 
 
 
 
35
 
36
+ # İçerde kalan noktaları bul
37
+ mask = df.apply(lambda row: polygon.contains(Point(row[x_col], row[y_col])), axis=1)
38
+ return df[mask]
39
 
40
  with gr.Blocks() as demo:
41
+ gr.Markdown("## Interaktif Veri Seçim Aracı")
42
 
43
+ df_state = gr.State(pd.DataFrame())
 
44
 
45
  with gr.Row():
46
  csv_upload = gr.File(label="CSV Yükle", file_types=[".csv"])
47
  x_col = gr.Dropdown(label="X Sütunu")
48
  y_col = gr.Dropdown(label="Y Sütunu")
49
 
50
+ plot = gr.ScatterPlot(
51
+ label="Veri Dağılımı",
52
+ show_export_button=True,
53
+ interactive=True
54
+ )
55
+ results = gr.DataFrame(label="Seçilen Veriler")
56
 
57
+ # CSV yükleme işlemi
58
  csv_upload.upload(
59
  load_csv,
60
  inputs=csv_upload,
 
65
  outputs=[x_col, y_col]
66
  )
67
 
68
+ # Sütun değişikliklerinde plot güncelleme
69
  x_col.change(
70
+ create_scatter,
71
+ inputs=[df_state, x_col, y_col],
72
  outputs=plot
73
  )
74
 
75
  y_col.change(
76
+ create_scatter,
77
+ inputs=[df_state, x_col, y_col],
78
  outputs=plot
79
  )
80
 
81
+ # Polygon seçim etkinliği
82
+ plot.select(
83
+ filter_points,
84
+ inputs=[df_state, x_col, y_col],
85
+ outputs=results
 
 
 
 
86
  )
87
 
88
  demo.launch()