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