jasvir-singh1021 commited on
Commit
cddef70
·
verified ·
1 Parent(s): 611317e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -48
app.py CHANGED
@@ -1,56 +1,58 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import plotly.express as px
4
-
5
- # Global variable to hold the dataset
6
- data = pd.DataFrame()
7
-
8
- def load_and_show(csv_file):
9
- global data
10
- if csv_file is None:
11
- return "Please upload a CSV file.", None, None
12
- data = pd.read_csv(csv_file.name)
13
- summary = data.describe().reset_index()
14
-
15
- return (
16
- data.head().to_dict(orient="records"), # Show first few rows in table
17
- summary.to_dict(orient="records"), # Show summary stats
18
- None # Empty plot initially
19
- )
20
-
21
- def plot_column(col_name):
22
- global data
23
- if data.empty or col_name not in data.columns:
24
- return None
25
- if pd.api.types.is_numeric_dtype(data[col_name]):
26
- fig = px.histogram(data, x=col_name, nbins=30, title=f"Distribution of {col_name}")
27
- return fig
28
  else:
29
- return None
 
 
 
 
30
 
31
  with gr.Blocks() as demo:
32
- gr.Markdown("# 📊 CSV Viewer with Search, Filter & Stats")
33
-
 
 
 
 
 
 
 
34
  with gr.Row():
35
- csv_input = gr.File(label="Upload CSV", file_types=[".csv"])
36
-
37
- table = gr.DataFrame(headers=None, interactive=True, label="Dataset Preview", max_rows=10)
38
- summary_table = gr.DataFrame(headers=None, interactive=False, label="Summary Statistics")
39
- plot = gr.Plot()
40
-
41
- csv_input.change(load_and_show, inputs=csv_input, outputs=[table, summary_table, plot])
42
-
43
- col_dropdown = gr.Dropdown(choices=[], label="Select column to plot histogram")
44
- col_dropdown.change(plot_column, inputs=col_dropdown, outputs=plot)
45
-
46
- # Update column dropdown choices after CSV load
47
- def update_columns(csv_file):
48
- if csv_file is None:
49
  return []
50
- df = pd.read_csv(csv_file.name)
51
- numeric_cols = [c for c in df.columns if pd.api.types.is_numeric_dtype(df[c])]
52
- return numeric_cols
53
-
54
- csv_input.change(update_columns, inputs=csv_input, outputs=col_dropdown)
55
-
 
56
  demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
+
4
+ # Global variable to store the dataframe
5
+ df_global = None
6
+
7
+ def load_data(file):
8
+ global df_global
9
+ if file is None:
10
+ return pd.DataFrame(), "Please upload a CSV file."
11
+ df_global = pd.read_csv(file.name)
12
+ preview = df_global.head(10)
13
+ return preview, f"Loaded {len(df_global)} rows and {len(df_global.columns)} columns."
14
+
15
+ def filter_data(column, value):
16
+ global df_global
17
+ if df_global is None:
18
+ return pd.DataFrame(), "No data loaded."
19
+ if column == "" or value == "":
20
+ filtered = df_global
 
 
 
 
 
 
 
21
  else:
22
+ # Filter rows where the column contains the value (case-insensitive)
23
+ filtered = df_global[df_global[column].astype(str).str.contains(value, case=False, na=False)]
24
+ preview = filtered.head(10)
25
+ summary = filtered.describe(include='all').to_string()
26
+ return preview, summary
27
 
28
  with gr.Blocks() as demo:
29
+ gr.Markdown("# CSV Data Explorer\nUpload a CSV, filter data, and see summary stats.")
30
+
31
+ with gr.Row():
32
+ csv_file = gr.File(label="Upload CSV", file_types=['.csv'])
33
+ load_btn = gr.Button("Load Data")
34
+
35
+ table = gr.DataFrame(headers=None, interactive=True, label="Data Preview")
36
+ status = gr.Textbox(label="Status", interactive=False)
37
+
38
  with gr.Row():
39
+ filter_column = gr.Dropdown(choices=[], label="Filter Column", interactive=True)
40
+ filter_value = gr.Textbox(placeholder="Filter value", label="Filter Value")
41
+ filter_btn = gr.Button("Filter Data")
42
+
43
+ summary_stats = gr.Textbox(label="Summary Statistics", interactive=False, lines=10)
44
+
45
+ load_btn.click(load_data, inputs=csv_file, outputs=[table, status])
46
+
47
+ # Update filter_column choices dynamically when data loads
48
+ def update_columns(file):
49
+ if file is None:
 
 
 
50
  return []
51
+ df = pd.read_csv(file.name)
52
+ return list(df.columns)
53
+
54
+ csv_file.change(update_columns, inputs=csv_file, outputs=filter_column)
55
+
56
+ filter_btn.click(filter_data, inputs=[filter_column, filter_value], outputs=[table, summary_stats])
57
+
58
  demo.launch()