Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,56 @@
|
|
| 1 |
-
from datasets import load_dataset
|
| 2 |
-
from tabulate import tabulate
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
dataset = load_dataset("csv", data_files="sample_data.csv")["train"]
|
| 8 |
-
table = tabulate(dataset[:5], headers="keys", tablefmt="github")
|
| 9 |
-
return table
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 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()
|