Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from shapely.geometry import Point, Polygon
|
| 4 |
|
| 5 |
def load_csv(file):
|
|
@@ -11,46 +12,45 @@ def update_dropdowns(df):
|
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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
|
| 26 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 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
|
| 43 |
|
| 44 |
df_state = gr.State(pd.DataFrame())
|
|
|
|
|
|
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
csv_upload = gr.File(label="Upload CSV", file_types=[".csv"])
|
| 48 |
x_col = gr.Dropdown(label="X Column")
|
| 49 |
y_col = gr.Dropdown(label="Y Column")
|
| 50 |
|
| 51 |
-
plot = gr.
|
| 52 |
-
results = gr.DataFrame(label="Points
|
| 53 |
|
|
|
|
| 54 |
csv_upload.upload(
|
| 55 |
load_csv,
|
| 56 |
inputs=csv_upload,
|
|
@@ -61,20 +61,31 @@ with gr.Blocks() as demo:
|
|
| 61 |
outputs=[x_col, y_col]
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
x_col.change(
|
| 65 |
create_plot,
|
| 66 |
inputs=[df_state, x_col, y_col],
|
| 67 |
outputs=plot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
|
|
|
| 69 |
y_col.change(
|
| 70 |
create_plot,
|
| 71 |
inputs=[df_state, x_col, y_col],
|
| 72 |
outputs=plot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
|
|
|
|
| 75 |
plot.select(
|
| 76 |
find_points_in_polygon,
|
| 77 |
-
inputs=[df_state,
|
| 78 |
outputs=results
|
| 79 |
)
|
| 80 |
|
|
|
|
| 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):
|
|
|
|
| 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)
|
| 35 |
|
| 36 |
+
return df[mask]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
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 |
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 |
|