Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,57 +8,46 @@ def load_data(file):
|
|
| 8 |
global df_global
|
| 9 |
if file is None:
|
| 10 |
return pd.DataFrame(), "Please upload a CSV file."
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def filter_data(column, value):
|
| 19 |
global df_global
|
| 20 |
if df_global is None:
|
| 21 |
-
return pd.DataFrame(), "
|
| 22 |
-
|
| 23 |
-
if not column or column not in df_global.columns:
|
| 24 |
-
return pd.DataFrame(), "β οΈ Please select a valid column."
|
| 25 |
-
|
| 26 |
-
if value.strip() == "":
|
| 27 |
filtered = df_global
|
| 28 |
else:
|
|
|
|
| 29 |
filtered = df_global[df_global[column].astype(str).str.contains(value, case=False, na=False)]
|
| 30 |
-
|
| 31 |
preview = filtered.head(10)
|
| 32 |
summary = filtered.describe(include='all').to_string()
|
| 33 |
return preview, summary
|
| 34 |
|
| 35 |
-
def update_columns(file):
|
| 36 |
-
if file is None:
|
| 37 |
-
return []
|
| 38 |
-
try:
|
| 39 |
-
df = pd.read_csv(file.name)
|
| 40 |
-
return list(df.columns)
|
| 41 |
-
except:
|
| 42 |
-
return []
|
| 43 |
-
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
-
gr.Markdown("# π CSV Data Explorer\nUpload a CSV file, filter
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
csv_file = gr.File(label="Upload CSV", file_types=[
|
| 49 |
-
load_btn = gr.Button("
|
| 50 |
|
| 51 |
table = gr.DataFrame(headers=None, interactive=True, label="π Data Preview")
|
| 52 |
-
status = gr.Textbox(label="Status", interactive=False)
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
-
filter_column = gr.Dropdown(choices=[], label="Filter Column", interactive=True
|
| 56 |
-
filter_value = gr.Textbox(placeholder="Enter value to
|
| 57 |
-
filter_btn = gr.Button("
|
| 58 |
|
| 59 |
summary_stats = gr.Textbox(label="π Summary Statistics", interactive=False, lines=10)
|
| 60 |
|
| 61 |
-
# Link buttons to functions
|
| 62 |
load_btn.click(load_data, inputs=csv_file, outputs=[table, status])
|
| 63 |
csv_file.change(update_columns, inputs=csv_file, outputs=filter_column)
|
| 64 |
filter_btn.click(filter_data, inputs=[filter_column, filter_value], outputs=[table, summary_stats])
|
|
|
|
| 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 update_columns(file):
|
| 16 |
+
if file is None:
|
| 17 |
+
return gr.update(choices=[])
|
| 18 |
+
df = pd.read_csv(file.name)
|
| 19 |
+
return gr.update(choices=list(df.columns))
|
| 20 |
|
| 21 |
def filter_data(column, value):
|
| 22 |
global df_global
|
| 23 |
if df_global is None:
|
| 24 |
+
return pd.DataFrame(), "No data loaded."
|
| 25 |
+
if column == "" or value == "":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
filtered = df_global
|
| 27 |
else:
|
| 28 |
+
# Filter rows where the column contains the value (case-insensitive)
|
| 29 |
filtered = df_global[df_global[column].astype(str).str.contains(value, case=False, na=False)]
|
|
|
|
| 30 |
preview = filtered.head(10)
|
| 31 |
summary = filtered.describe(include='all').to_string()
|
| 32 |
return preview, summary
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# π CSV Data Explorer\nUpload a CSV file, filter rows by column value, and view summary statistics.")
|
| 36 |
|
| 37 |
with gr.Row():
|
| 38 |
+
csv_file = gr.File(label="π Upload CSV", file_types=['.csv'])
|
| 39 |
+
load_btn = gr.Button("π€ Load Data")
|
| 40 |
|
| 41 |
table = gr.DataFrame(headers=None, interactive=True, label="π Data Preview")
|
| 42 |
+
status = gr.Textbox(label="β
Status", interactive=False)
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
+
filter_column = gr.Dropdown(choices=[], label="π½ Filter Column", interactive=True)
|
| 46 |
+
filter_value = gr.Textbox(placeholder="Enter value to match", label="π Filter Value")
|
| 47 |
+
filter_btn = gr.Button("π§Ή Filter Data")
|
| 48 |
|
| 49 |
summary_stats = gr.Textbox(label="π Summary Statistics", interactive=False, lines=10)
|
| 50 |
|
|
|
|
| 51 |
load_btn.click(load_data, inputs=csv_file, outputs=[table, status])
|
| 52 |
csv_file.change(update_columns, inputs=csv_file, outputs=filter_column)
|
| 53 |
filter_btn.click(filter_data, inputs=[filter_column, filter_value], outputs=[table, summary_stats])
|