Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,10 @@ import io
|
|
| 6 |
|
| 7 |
# --- 1. Function to process the uploaded file and generate stats/dataframe ---
|
| 8 |
def analyze_csv(file):
|
|
|
|
| 9 |
if file is None:
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
try:
|
| 13 |
# Read the uploaded file object (it's a temporary file path)
|
|
@@ -16,23 +18,24 @@ def analyze_csv(file):
|
|
| 16 |
# Basic descriptive statistics
|
| 17 |
stats = df.describe().round(2).T
|
| 18 |
|
| 19 |
-
# Convert stats to
|
| 20 |
stats_markdown = stats.to_markdown()
|
| 21 |
|
| 22 |
# Get list of numeric columns for the dropdown
|
| 23 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 24 |
|
| 25 |
-
# Return the full DataFrame, the statistics table, and the
|
| 26 |
return df, stats_markdown, gr.Dropdown(choices=numeric_cols, label="Select Column to Plot"), df
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
error_message = f"Error processing file: {e}"
|
| 30 |
-
# Return
|
| 31 |
return None, error_message, gr.Dropdown(choices=[], label="Select Column to Plot"), None
|
| 32 |
|
| 33 |
|
| 34 |
# --- 2. Function to generate a plot for a selected column ---
|
| 35 |
def generate_plot(df_state, column_name):
|
|
|
|
| 36 |
if df_state is None or column_name is None or column_name == "":
|
| 37 |
return None
|
| 38 |
|
|
@@ -40,7 +43,7 @@ def generate_plot(df_state, column_name):
|
|
| 40 |
# Reset Matplotlib figure for a clean slate
|
| 41 |
plt.figure(figsize=(8, 5))
|
| 42 |
|
| 43 |
-
# Use a Histogram for distribution of the selected column
|
| 44 |
sns.histplot(df_state[column_name], kde=True)
|
| 45 |
|
| 46 |
plt.title(f'Distribution of {column_name}')
|
|
@@ -66,6 +69,7 @@ with gr.Blocks(title="CSV Data Analyzer") as demo:
|
|
| 66 |
gr.Markdown("Upload your CSV file and see instant statistics and visualizations.")
|
| 67 |
|
| 68 |
# State component to hold the DataFrame across function calls
|
|
|
|
| 69 |
df_state = gr.State(None)
|
| 70 |
|
| 71 |
# Input components
|
|
@@ -73,7 +77,8 @@ with gr.Blocks(title="CSV Data Analyzer") as demo:
|
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
# Outputs
|
| 76 |
-
|
|
|
|
| 77 |
stats_output = gr.Markdown(label="Descriptive Statistics")
|
| 78 |
|
| 79 |
gr.HTML("<hr>")
|
|
@@ -89,14 +94,15 @@ with gr.Blocks(title="CSV Data Analyzer") as demo:
|
|
| 89 |
|
| 90 |
# --- 4. Event Handling (Interactions) ---
|
| 91 |
|
| 92 |
-
#
|
| 93 |
csv_file.upload(
|
| 94 |
analyze_csv,
|
| 95 |
inputs=[csv_file],
|
|
|
|
| 96 |
outputs=[df_output, stats_output, column_dropdown, df_state]
|
| 97 |
)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
plot_button.click(
|
| 101 |
generate_plot,
|
| 102 |
inputs=[df_state, column_dropdown],
|
|
|
|
| 6 |
|
| 7 |
# --- 1. Function to process the uploaded file and generate stats/dataframe ---
|
| 8 |
def analyze_csv(file):
|
| 9 |
+
"""Reads a CSV file, calculates statistics, and prepares components for plotting."""
|
| 10 |
if file is None:
|
| 11 |
+
# Clear previous outputs if a file is unuploaded or if this is the initial state
|
| 12 |
+
return None, "Please upload a CSV file.", gr.Dropdown(choices=[], label="Select Column to Plot"), None
|
| 13 |
|
| 14 |
try:
|
| 15 |
# Read the uploaded file object (it's a temporary file path)
|
|
|
|
| 18 |
# Basic descriptive statistics
|
| 19 |
stats = df.describe().round(2).T
|
| 20 |
|
| 21 |
+
# Convert stats to markdown string for display
|
| 22 |
stats_markdown = stats.to_markdown()
|
| 23 |
|
| 24 |
# Get list of numeric columns for the dropdown
|
| 25 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 26 |
|
| 27 |
+
# Return the full DataFrame, the statistics table, the updated column dropdown, and the DataFrame state
|
| 28 |
return df, stats_markdown, gr.Dropdown(choices=numeric_cols, label="Select Column to Plot"), df
|
| 29 |
|
| 30 |
except Exception as e:
|
| 31 |
error_message = f"Error processing file: {e}"
|
| 32 |
+
# Return error message and clear other components
|
| 33 |
return None, error_message, gr.Dropdown(choices=[], label="Select Column to Plot"), None
|
| 34 |
|
| 35 |
|
| 36 |
# --- 2. Function to generate a plot for a selected column ---
|
| 37 |
def generate_plot(df_state, column_name):
|
| 38 |
+
"""Generates a distribution plot (histogram) for the selected column."""
|
| 39 |
if df_state is None or column_name is None or column_name == "":
|
| 40 |
return None
|
| 41 |
|
|
|
|
| 43 |
# Reset Matplotlib figure for a clean slate
|
| 44 |
plt.figure(figsize=(8, 5))
|
| 45 |
|
| 46 |
+
# Use a Histogram for distribution of the selected numeric column
|
| 47 |
sns.histplot(df_state[column_name], kde=True)
|
| 48 |
|
| 49 |
plt.title(f'Distribution of {column_name}')
|
|
|
|
| 69 |
gr.Markdown("Upload your CSV file and see instant statistics and visualizations.")
|
| 70 |
|
| 71 |
# State component to hold the DataFrame across function calls
|
| 72 |
+
# This is essential for passing the DataFrame from `analyze_csv` to `generate_plot`
|
| 73 |
df_state = gr.State(None)
|
| 74 |
|
| 75 |
# Input components
|
|
|
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
# Outputs
|
| 80 |
+
# **FIXED:** Removed the unsupported 'height=200' argument.
|
| 81 |
+
df_output = gr.Dataframe(label="Uploaded Data Preview", interactive=False)
|
| 82 |
stats_output = gr.Markdown(label="Descriptive Statistics")
|
| 83 |
|
| 84 |
gr.HTML("<hr>")
|
|
|
|
| 94 |
|
| 95 |
# --- 4. Event Handling (Interactions) ---
|
| 96 |
|
| 97 |
+
# Triggered when a file is uploaded (or cleared)
|
| 98 |
csv_file.upload(
|
| 99 |
analyze_csv,
|
| 100 |
inputs=[csv_file],
|
| 101 |
+
# Note: The output for the dropdown component is updated with new choices here.
|
| 102 |
outputs=[df_output, stats_output, column_dropdown, df_state]
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# Triggered when the plot button is clicked
|
| 106 |
plot_button.click(
|
| 107 |
generate_plot,
|
| 108 |
inputs=[df_state, column_dropdown],
|