Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,8 @@ def upload_csv(file):
|
|
| 14 |
if file is None:
|
| 15 |
return ["All"], ["All"], "No file uploaded. Please upload a CSV file.", "All", "All"
|
| 16 |
|
| 17 |
-
# Read the CSV file
|
| 18 |
-
df = pd.read_csv(file
|
| 19 |
|
| 20 |
# Check if the DataFrame is empty
|
| 21 |
if df.empty:
|
|
@@ -29,11 +29,16 @@ def upload_csv(file):
|
|
| 29 |
missing_cols = required_columns - set(df.columns)
|
| 30 |
return ["All"], ["All"], f"CSV is missing required columns: {', '.join(missing_cols)}", "All", "All"
|
| 31 |
|
| 32 |
-
# Extract unique values for dropdowns
|
| 33 |
-
labs = ['All'] + sorted(df['Lab'].dropna().unique().
|
| 34 |
-
types = ['All'] + sorted(df['Type'].dropna().unique().
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# Return the updated choices and reset the dropdown values to "All"
|
| 37 |
return labs, types, "", "All", "All"
|
| 38 |
except Exception as e:
|
| 39 |
# Handle any other errors (e.g., file not readable, invalid CSV format)
|
|
|
|
| 14 |
if file is None:
|
| 15 |
return ["All"], ["All"], "No file uploaded. Please upload a CSV file.", "All", "All"
|
| 16 |
|
| 17 |
+
# Read the CSV file directly from the file object
|
| 18 |
+
df = pd.read_csv(file)
|
| 19 |
|
| 20 |
# Check if the DataFrame is empty
|
| 21 |
if df.empty:
|
|
|
|
| 29 |
missing_cols = required_columns - set(df.columns)
|
| 30 |
return ["All"], ["All"], f"CSV is missing required columns: {', '.join(missing_cols)}", "All", "All"
|
| 31 |
|
| 32 |
+
# Extract unique values for dropdowns, ensuring no empty or invalid entries
|
| 33 |
+
labs = ['All'] + sorted([str(lab) for lab in df['Lab'].dropna().unique() if str(lab).strip()])
|
| 34 |
+
types = ['All'] + sorted([str(type_) for type_ in df['Type'].dropna().unique() if str(type_).strip()])
|
| 35 |
+
|
| 36 |
+
# If no valid labs or types are found, return default values
|
| 37 |
+
if len(labs) == 1: # Only "All" is present
|
| 38 |
+
labs = ["All"]
|
| 39 |
+
if len(types) == 1: # Only "All" is present
|
| 40 |
+
types = ["All"]
|
| 41 |
|
|
|
|
| 42 |
return labs, types, "", "All", "All"
|
| 43 |
except Exception as e:
|
| 44 |
# Handle any other errors (e.g., file not readable, invalid CSV format)
|