data-explorer / app.py
jasvir-singh1021's picture
Update app.py
725c5c1 verified
import gradio as gr
import pandas as pd
# Global variable to store the dataframe
df_global = None
def load_data(file):
global df_global
if file is None:
return pd.DataFrame(), "Please upload a CSV file."
df_global = pd.read_csv(file.name)
preview = df_global.head(10)
return preview, f"Loaded {len(df_global)} rows and {len(df_global.columns)} columns."
def update_columns(file):
if file is None:
return gr.update(choices=[])
df = pd.read_csv(file.name)
return gr.update(choices=list(df.columns))
def filter_data(column, value):
global df_global
if df_global is None:
return pd.DataFrame(), "No data loaded."
if column == "" or value == "":
filtered = df_global
else:
# Filter rows where the column contains the value (case-insensitive)
filtered = df_global[df_global[column].astype(str).str.contains(value, case=False, na=False)]
preview = filtered.head(10)
summary = filtered.describe(include='all').to_string()
return preview, summary
with gr.Blocks() as demo:
gr.Markdown("# πŸ“Š CSV Data Explorer\nUpload a CSV file, filter rows by column value, and view summary statistics.")
with gr.Row():
csv_file = gr.File(label="πŸ“ Upload CSV", file_types=['.csv'])
load_btn = gr.Button("πŸ“€ Load Data")
table = gr.DataFrame(headers=None, interactive=True, label="πŸ” Data Preview")
status = gr.Textbox(label="βœ… Status", interactive=False)
with gr.Row():
filter_column = gr.Dropdown(choices=[], label="πŸ”½ Filter Column", interactive=True)
filter_value = gr.Textbox(placeholder="Enter value to match", label="πŸ”Ž Filter Value")
filter_btn = gr.Button("🧹 Filter Data")
summary_stats = gr.Textbox(label="πŸ“ˆ Summary Statistics", interactive=False, lines=10)
load_btn.click(load_data, inputs=csv_file, outputs=[table, status])
csv_file.change(update_columns, inputs=csv_file, outputs=filter_column)
filter_btn.click(filter_data, inputs=[filter_column, filter_value], outputs=[table, summary_stats])
demo.launch()