Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
-
gr.Markdown("#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Row():
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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(
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 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()
|