File size: 2,228 Bytes
c6379ae
9be415c
b13bccc
c6379ae
b13bccc
 
e94f3be
b13bccc
 
 
f305b38
b13bccc
 
 
 
 
 
 
 
602a264
b13bccc
 
 
 
 
 
 
 
 
 
 
 
 
 
f305b38
da71915
b13bccc
ee16303
 
b13bccc
 
 
ee16303
da71915
b13bccc
 
9239ab3
b13bccc
da71915
b13bccc
 
da71915
b13bccc
da71915
9239ab3
b13bccc
 
 
 
 
 
 
 
da71915
b13bccc
 
 
 
 
 
 
 
 
9239ab3
f305b38
b13bccc
 
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
import gradio as gr
import pandas as pd
from vega_datasets import data

def update_dropdowns(csv_file):
    if csv_file is None:
        return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
    df = pd.read_csv(csv_file.name)
    numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
    return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols)

def plot_selection(selection, df):
    if selection is None or df is None:
        return pd.DataFrame()
    return df.iloc[[i["index"] for i in selection]]

def create_scatter_plot(csv_file, x_col, y_col):
    if csv_file is None or x_col is None or y_col is None:
        return None, None
    
    df = pd.read_csv(csv_file.name)
    plot = gr.ScatterPlot(
        value=df,
        x=x_col,
        y=y_col,
        title="Custom CSV Scatter Plot",
        x_title=x_col,
        y_title=y_col,
        tooltip=df.columns.tolist(),
        color_legend_title="Values",
        interactive=True,
        brush_radius=5  # Lasso/polygon seçim için
    )
    return plot, df

with gr.Blocks() as app:
    gr.Markdown("## 📊 Custom CSV Scatter Plot with Selection")
    
    with gr.Row():
        csv_upload = gr.UploadButton(label="Upload CSV", file_types=[".csv"])
        x_axis = gr.Dropdown(label="X Axis Column")
        y_axis = gr.Dropdown(label="Y Axis Column")
    
    with gr.Row():
        plot = gr.ScatterPlot(interactive=True, brush_radius=5)
        selected_data = gr.DataFrame(label="Selected Points")
    
    df_store = gr.State()

    # CSV yüklendiğinde dropdown'ları güncelle
    csv_upload.upload(
        fn=update_dropdowns,
        inputs=csv_upload,
        outputs=[x_axis, y_axis]
    )

    # Eksenler seçildiğinde grafiği oluştur
    x_axis.change(
        fn=create_scatter_plot,
        inputs=[csv_upload, x_axis, y_axis],
        outputs=[plot, df_store]
    )
    y_axis.change(
        fn=create_scatter_plot,
        inputs=[csv_upload, x_axis, y_axis],
        outputs=[plot, df_store]
    )

    # Seçim yapıldığında verileri güncelle
    plot.select(
        fn=plot_selection,
        inputs=df_store,
        outputs=selected_data
    )

if __name__ == "__main__":
    app.launch()