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()