File size: 2,428 Bytes
c6379ae
9be415c
9239ab3
c6379ae
9239ab3
 
 
 
e94f3be
 
 
 
f305b38
9445aeb
e94f3be
c12e9d0
9445aeb
 
 
 
49eba4a
 
 
9445aeb
f305b38
9445aeb
e94f3be
 
 
9445aeb
 
602a264
9445aeb
 
 
c12e9d0
602a264
e94f3be
 
 
 
 
 
 
 
 
f305b38
 
e94f3be
ee16303
9445aeb
ee16303
 
9445aeb
 
 
ee16303
c12e9d0
49eba4a
9445aeb
c12e9d0
 
ee16303
49eba4a
9445aeb
9239ab3
67adc66
9239ab3
 
 
 
 
 
 
 
 
9445aeb
c12e9d0
9239ab3
 
 
9445aeb
c12e9d0
9239ab3
 
 
c12e9d0
 
 
 
9239ab3
f305b38
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import gradio as gr
import pandas as pd
from shapely.geometry import Point, Polygon

def load_csv(file):
    return pd.read_csv(file.name)

def update_dropdowns(df):
    if df is None or df.empty:
        return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
    columns = df.columns.tolist()  # Index'i listeye çevir
    return gr.Dropdown(choices=columns), gr.Dropdown(choices=columns)

def create_plot(df, x_col, y_col):
    if df is None or x_col not in df.columns or y_col not in df.columns:
        return None
    return {
        "x": df[x_col].tolist(),
        "y": df[y_col].tolist(),
        "color": "blue",
        "tooltip": df.columns.tolist(),
        "x_title": x_col,
        "y_title": y_col
    }

def filter_points(event: gr.SelectData, df, x_col, y_col):
    if df is None or x_col not in df.columns or y_col not in df.columns:
        return pd.DataFrame()
    
    if not hasattr(filter_points, "points"):
        filter_points.points = []
    
    filter_points.points.append((event.x, event.y))
    
    if len(filter_points.points) < 3:
        return pd.DataFrame()
    
    try:
        polygon = Polygon(filter_points.points)
        mask = df[[x_col, y_col]].apply(
            lambda row: polygon.contains(Point(row[x_col], row[y_col])), 
            axis=1
        )
        return df[mask]
    finally:
        filter_points.points = []

with gr.Blocks() as demo:
    gr.Markdown("## 🎯 Çalışan Son Sürüm")
    
    df_state = gr.State()
    
    with gr.Row():
        csv_upload = gr.File(label="1. CSV Yükle", file_types=[".csv"])
        x_col = gr.Dropdown(label="2. X Sütunu Seç")
        y_col = gr.Dropdown(label="3. Y Sütunu Seç")
    
    plot = gr.ScatterPlot(
        label="4. Lasso Tool ile Alan Seç",
        show_label=True,
        interactive=True
    )
    
    results = gr.DataFrame(label="Seçilen Veriler")
    
    csv_upload.upload(
        load_csv,
        inputs=csv_upload,
        outputs=df_state
    ).then(
        update_dropdowns,
        inputs=df_state,
        outputs=[x_col, y_col]
    )
    
    x_col.change(
        create_plot,
        inputs=[df_state, x_col, y_col],
        outputs=plot
    )
    y_col.change(
        create_plot,
        inputs=[df_state, x_col, y_col],
        outputs=plot
    )
    
    plot.select(
        filter_points,
        inputs=[df_state, x_col, y_col],
        outputs=results
    )

demo.launch()