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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -28
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import plotly.express as px
4
  from shapely.geometry import Point, Polygon
 
5
 
6
  def load_csv(file):
7
  return pd.read_csv(file.name)
@@ -12,23 +12,25 @@ def update_dropdowns(df):
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
-
16
- fig = px.scatter(df, x=x_col, y=y_col, title=f"{x_col} vs {y_col}")
17
- fig.update_layout(dragmode='lasso')
18
- return fig
 
 
 
 
 
19
 
20
  def find_points_in_polygon(selected_data, df, x_col, y_col):
21
- if not selected_data or not df.empty:
22
  return pd.DataFrame()
23
 
24
- # Get selected points from Plotly's lasso selection
25
- selected_points = selected_data['points']
26
- if not selected_points:
27
  return pd.DataFrame()
28
 
29
- # Extract coordinates of polygon vertices
30
- polygon_points = [(p['x'], p['y']) for p in selected_points]
31
-
32
  # Create polygon and check containment
33
  polygon = Polygon(polygon_points)
34
  mask = df.apply(lambda row: polygon.contains(Point(row[x_col], row[y_col])), axis=1)
@@ -39,18 +41,19 @@ with gr.Blocks() as demo:
39
  gr.Markdown("## Interactive CSV Explorer with Polygon Selection")
40
 
41
  df_state = gr.State(pd.DataFrame())
42
- x_col_state = gr.State()
43
- y_col_state = gr.State()
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.Plot(label="Scatter Plot")
51
- results = gr.DataFrame(label="Selected Points")
 
 
 
 
52
 
53
- # Upload handling
54
  csv_upload.upload(
55
  load_csv,
56
  inputs=csv_upload,
@@ -61,31 +64,21 @@ with gr.Blocks() as demo:
61
  outputs=[x_col, y_col]
62
  )
63
 
64
- # Plot updates
65
  x_col.change(
66
  create_plot,
67
  inputs=[df_state, x_col, y_col],
68
  outputs=plot
69
- ).then(
70
- lambda x: x, # Store x_col in state
71
- inputs=x_col,
72
- outputs=x_col_state
73
  )
74
 
75
  y_col.change(
76
  create_plot,
77
  inputs=[df_state, x_col, y_col],
78
  outputs=plot
79
- ).then(
80
- lambda y: y, # Store y_col in state
81
- inputs=y_col,
82
- outputs=y_col_state
83
  )
84
 
85
- # Selection handling
86
  plot.select(
87
  find_points_in_polygon,
88
- inputs=[gr.State(), df_state, x_col_state, y_col_state],
89
  outputs=results
90
  )
91
 
 
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)
 
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)
 
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,
59
  inputs=csv_upload,
 
64
  outputs=[x_col, y_col]
65
  )
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