Spaces:
Runtime error
Runtime error
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() |