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 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
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
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
|
44 |
-
if not
|
45 |
-
return
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
-
# Polygon
|
52 |
-
|
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 |
-
|
|
|
|
|
61 |
|
62 |
with gr.Blocks() as demo:
|
63 |
-
gr.Markdown("## Interaktif
|
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.
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
# CSV yükleme
|
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 |
-
#
|
88 |
x_col.change(
|
89 |
-
|
90 |
-
inputs=[df_state, x_col, y_col
|
91 |
outputs=plot
|
92 |
)
|
93 |
|
94 |
y_col.change(
|
95 |
-
|
96 |
-
inputs=[df_state, x_col, y_col
|
97 |
outputs=plot
|
98 |
)
|
99 |
|
100 |
-
#
|
101 |
-
plot.
|
102 |
-
|
103 |
-
inputs=[df_state, x_col, y_col
|
104 |
-
outputs=
|
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()
|