Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
from shapely.geometry import Point, Polygon
|
5 |
|
6 |
def load_csv(file):
|
@@ -12,37 +11,37 @@ 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 |
-
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 |
)
|
23 |
|
24 |
def find_points_in_polygon(selected_data, df, x_col, y_col):
|
25 |
-
if not selected_data or not x_col or not y_col:
|
26 |
-
return
|
27 |
-
|
28 |
-
# Extract polygon points from selection
|
29 |
-
polygon_points = [(point["x"], point["y"]) for point in selected_data["points"]]
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
with gr.Blocks() as demo:
|
41 |
gr.Markdown("## CSV Data Explorer with Polygon Selection")
|
42 |
|
43 |
-
|
44 |
-
df_state = gr.State()
|
45 |
-
polygon_state = gr.State([])
|
46 |
|
47 |
with gr.Row():
|
48 |
csv_upload = gr.File(label="Upload CSV", file_types=[".csv"])
|
@@ -50,11 +49,10 @@ with gr.Blocks() as demo:
|
|
50 |
y_col = gr.Dropdown(label="Y Column")
|
51 |
|
52 |
plot = gr.ScatterPlot(interactive=True, label="Scatter Plot")
|
53 |
-
results = gr.DataFrame(label="Points in Polygon")
|
54 |
|
55 |
-
# Event handlers
|
56 |
csv_upload.upload(
|
57 |
-
load_csv,
|
58 |
inputs=csv_upload,
|
59 |
outputs=df_state
|
60 |
).then(
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
3 |
from shapely.geometry import Point, Polygon
|
4 |
|
5 |
def load_csv(file):
|
|
|
11 |
def create_plot(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=pd.DataFrame({x_col: df[x_col], y_col: df[y_col]}),
|
16 |
x=x_col,
|
17 |
y=y_col,
|
18 |
title=f"{x_col} vs {y_col}",
|
19 |
interactive=True,
|
20 |
+
tooltip=[x_col, y_col],
|
21 |
+
elem_id="scatter"
|
22 |
)
|
23 |
|
24 |
def find_points_in_polygon(selected_data, df, x_col, y_col):
|
25 |
+
if not selected_data or df.empty or not x_col or not y_col:
|
26 |
+
return gr.DataFrame()
|
|
|
|
|
|
|
27 |
|
28 |
+
try:
|
29 |
+
polygon_points = [(p["x"], p["y"]) for p in selected_data["points"]]
|
30 |
+
if len(polygon_points) < 3:
|
31 |
+
return gr.DataFrame()
|
32 |
+
|
33 |
+
polygon = Polygon(polygon_points)
|
34 |
+
points = df[[x_col, y_col]].apply(lambda row: Point(row[x_col], row[y_col]), axis=1)
|
35 |
+
mask = points.apply(lambda p: polygon.contains(p))
|
36 |
+
return df[mask]
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Error: {e}")
|
39 |
+
return gr.DataFrame()
|
40 |
|
41 |
with gr.Blocks() as demo:
|
42 |
gr.Markdown("## CSV Data Explorer with Polygon Selection")
|
43 |
|
44 |
+
df_state = gr.State(pd.DataFrame())
|
|
|
|
|
45 |
|
46 |
with gr.Row():
|
47 |
csv_upload = gr.File(label="Upload CSV", file_types=[".csv"])
|
|
|
49 |
y_col = gr.Dropdown(label="Y Column")
|
50 |
|
51 |
plot = gr.ScatterPlot(interactive=True, label="Scatter Plot")
|
52 |
+
results = gr.DataFrame(label="Points in Polygon", render=False)
|
53 |
|
|
|
54 |
csv_upload.upload(
|
55 |
+
load_csv,
|
56 |
inputs=csv_upload,
|
57 |
outputs=df_state
|
58 |
).then(
|