Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,77 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
|
4 |
|
5 |
-
def update_dropdowns(
|
6 |
-
if
|
7 |
return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
|
8 |
-
df = pd.read_csv(
|
9 |
-
|
10 |
-
return gr.Dropdown(choices=
|
11 |
|
12 |
-
def
|
13 |
-
if
|
14 |
-
return
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
plt.title(f"{x_col} vs {y_col}")
|
21 |
-
plt.xlabel(x_col)
|
22 |
-
plt.ylabel(y_col)
|
23 |
-
plt.grid(True)
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
with gr.Blocks() as app:
|
29 |
-
gr.Markdown("
|
30 |
|
31 |
with gr.Row():
|
32 |
-
|
|
|
|
|
33 |
|
34 |
with gr.Row():
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
plot_button = gr.Button("Grafiği Oluştur")
|
39 |
|
40 |
-
|
41 |
-
plot_output = gr.Image(label="Scatter Plot")
|
42 |
|
43 |
-
|
|
|
44 |
fn=update_dropdowns,
|
45 |
-
inputs=
|
46 |
outputs=[x_axis, y_axis]
|
47 |
)
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
fn=create_scatter_plot,
|
51 |
-
inputs=[
|
52 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
from vega_datasets import data
|
4 |
|
5 |
+
def update_dropdowns(csv_file):
|
6 |
+
if csv_file is None:
|
7 |
return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
|
8 |
+
df = pd.read_csv(csv_file.name)
|
9 |
+
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
10 |
+
return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols)
|
11 |
|
12 |
+
def plot_selection(selection, df):
|
13 |
+
if selection is None or df is None:
|
14 |
+
return pd.DataFrame()
|
15 |
+
return df.iloc[[i["index"] for i in selection]]
|
16 |
+
|
17 |
+
def create_scatter_plot(csv_file, x_col, y_col):
|
18 |
+
if csv_file is None or x_col is None or y_col is None:
|
19 |
+
return None, None
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
df = pd.read_csv(csv_file.name)
|
22 |
+
plot = gr.ScatterPlot(
|
23 |
+
value=df,
|
24 |
+
x=x_col,
|
25 |
+
y=y_col,
|
26 |
+
title="Custom CSV Scatter Plot",
|
27 |
+
x_title=x_col,
|
28 |
+
y_title=y_col,
|
29 |
+
tooltip=df.columns.tolist(),
|
30 |
+
color_legend_title="Values",
|
31 |
+
interactive=True,
|
32 |
+
brush_radius=5 # Lasso/polygon seçim için
|
33 |
+
)
|
34 |
+
return plot, df
|
35 |
|
36 |
with gr.Blocks() as app:
|
37 |
+
gr.Markdown("## 📊 Custom CSV Scatter Plot with Selection")
|
38 |
|
39 |
with gr.Row():
|
40 |
+
csv_upload = gr.UploadButton(label="Upload CSV", file_types=[".csv"])
|
41 |
+
x_axis = gr.Dropdown(label="X Axis Column")
|
42 |
+
y_axis = gr.Dropdown(label="Y Axis Column")
|
43 |
|
44 |
with gr.Row():
|
45 |
+
plot = gr.ScatterPlot(interactive=True, brush_radius=5)
|
46 |
+
selected_data = gr.DataFrame(label="Selected Points")
|
|
|
|
|
47 |
|
48 |
+
df_store = gr.State()
|
|
|
49 |
|
50 |
+
# CSV yüklendiğinde dropdown'ları güncelle
|
51 |
+
csv_upload.upload(
|
52 |
fn=update_dropdowns,
|
53 |
+
inputs=csv_upload,
|
54 |
outputs=[x_axis, y_axis]
|
55 |
)
|
56 |
+
|
57 |
+
# Eksenler seçildiğinde grafiği oluştur
|
58 |
+
x_axis.change(
|
59 |
+
fn=create_scatter_plot,
|
60 |
+
inputs=[csv_upload, x_axis, y_axis],
|
61 |
+
outputs=[plot, df_store]
|
62 |
+
)
|
63 |
+
y_axis.change(
|
64 |
fn=create_scatter_plot,
|
65 |
+
inputs=[csv_upload, x_axis, y_axis],
|
66 |
+
outputs=[plot, df_store]
|
67 |
+
)
|
68 |
+
|
69 |
+
# Seçim yapıldığında verileri güncelle
|
70 |
+
plot.select(
|
71 |
+
fn=plot_selection,
|
72 |
+
inputs=df_store,
|
73 |
+
outputs=selected_data
|
74 |
)
|
75 |
|
76 |
+
if __name__ == "__main__":
|
77 |
+
app.launch()
|