File size: 2,111 Bytes
58f9421
e31f840
cddef70
 
 
 
 
 
 
 
725c5c1
 
 
 
 
 
 
 
 
cddef70
 
 
 
725c5c1
 
cddef70
e31f840
725c5c1
cddef70
 
 
 
58f9421
e31f840
725c5c1
cddef70
 
725c5c1
 
cddef70
e74c215
725c5c1
cddef70
e31f840
725c5c1
 
 
cddef70
e74c215
cddef70
 
 
 
 
18e96cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()