dotBlood / app.py
kedimestan's picture
Update app.py
e94f3be verified
raw
history blame
2.43 kB
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()