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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -35
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
  from shapely.geometry import Point, Polygon
4
- import numpy as np
5
 
6
  def load_csv(file):
7
  return pd.read_csv(file.name)
@@ -9,50 +9,69 @@ 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):
13
  if df is None or x_col is None or y_col is None:
14
  return None
15
- return gr.ScatterPlot(
16
- value=pd.DataFrame({x_col: df[x_col], y_col: df[y_col]}),
17
- x=x_col,
18
- y=y_col,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  title=f"{x_col} vs {y_col}",
20
- interactive=True,
21
- tooltip=[x_col, y_col],
22
- color="blue"
23
  )
 
 
24
 
25
- def find_points_in_polygon(selected_data, df, x_col, y_col):
26
- if not selected_data or df.empty or not x_col or not y_col:
27
- return pd.DataFrame()
28
 
29
- # Get selected polygon coordinates from lasso tool
30
- polygon_points = [(p["x"], p["y"]) for p in selected_data["points"]]
31
- if len(polygon_points) < 3:
32
- return pd.DataFrame()
33
 
34
- # Create polygon and check containment
35
- polygon = Polygon(polygon_points)
36
- mask = df.apply(lambda row: polygon.contains(Point(row[x_col], row[y_col])), axis=1)
 
 
37
 
38
- return df[mask]
39
 
40
  with gr.Blocks() as demo:
41
- gr.Markdown("## Interactive CSV Explorer with Polygon Selection")
42
 
43
  df_state = gr.State(pd.DataFrame())
 
44
 
45
  with gr.Row():
46
- csv_upload = gr.File(label="Upload CSV", file_types=[".csv"])
47
- x_col = gr.Dropdown(label="X Column")
48
- y_col = gr.Dropdown(label="Y Column")
49
 
50
- plot = gr.ScatterPlot(
51
- interactive=True,
52
- label="Scatter Plot",
53
- show_download_button=True
54
- )
55
- results = gr.DataFrame(label="Points in Polygon")
56
 
57
  csv_upload.upload(
58
  load_csv,
@@ -66,20 +85,20 @@ with gr.Blocks() as demo:
66
 
67
  x_col.change(
68
  create_plot,
69
- inputs=[df_state, x_col, y_col],
70
  outputs=plot
71
  )
72
 
73
  y_col.change(
74
  create_plot,
75
- inputs=[df_state, x_col, y_col],
76
  outputs=plot
77
  )
78
 
79
  plot.select(
80
- find_points_in_polygon,
81
- inputs=[df_state, x_col, y_col],
82
- outputs=results
83
  )
84
 
85
  demo.launch()
 
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):
7
  return pd.read_csv(file.name)
 
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
  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():
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="Scatter Plot ve Polygon")
74
+ results = gr.DataFrame(label="Seçilen Veriler")
 
 
 
 
75
 
76
  csv_upload.upload(
77
  load_csv,
 
85
 
86
  x_col.change(
87
  create_plot,
88
+ inputs=[df_state, x_col, y_col, polygon_state],
89
  outputs=plot
90
  )
91
 
92
  y_col.change(
93
  create_plot,
94
+ inputs=[df_state, x_col, y_col, polygon_state],
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()