Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,51 +8,59 @@ 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 |
def filter_data(column, value):
|
| 16 |
global df_global
|
| 17 |
if df_global is None:
|
| 18 |
-
return pd.DataFrame(), "No data loaded."
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 30 |
|
| 31 |
with gr.Row():
|
| 32 |
-
csv_file = gr.File(label="Upload CSV", file_types=[
|
| 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="
|
| 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()
|
|
|
|
| 8 |
global df_global
|
| 9 |
if file is None:
|
| 10 |
return pd.DataFrame(), "Please upload a CSV file."
|
| 11 |
+
try:
|
| 12 |
+
df_global = pd.read_csv(file.name)
|
| 13 |
+
preview = df_global.head(10)
|
| 14 |
+
return preview, f"β
Loaded {len(df_global)} rows and {len(df_global.columns)} columns."
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return pd.DataFrame(), f"β Error loading file: {e}"
|
| 17 |
|
| 18 |
def filter_data(column, value):
|
| 19 |
global df_global
|
| 20 |
if df_global is None:
|
| 21 |
+
return pd.DataFrame(), "β οΈ No data loaded yet."
|
| 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 it, and view summary statistics.")
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
+
csv_file = gr.File(label="Upload CSV", file_types=[".csv"])
|
| 49 |
+
load_btn = gr.Button("π₯ Load Data")
|
| 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, allow_custom_value=False)
|
| 56 |
+
filter_value = gr.Textbox(placeholder="Enter value to search...", label="Filter Value")
|
| 57 |
+
filter_btn = gr.Button("π Filter Data")
|
| 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])
|
| 65 |
|
| 66 |
demo.launch()
|