Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,14 +20,26 @@ def upload_csv(file):
|
|
| 20 |
if df.empty:
|
| 21 |
return ["All"], ["All"], ["All"], "The uploaded CSV file is empty.", "All", "All", "All"
|
| 22 |
|
| 23 |
-
# Define required columns
|
| 24 |
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
| 25 |
if not required_columns.issubset(df.columns):
|
| 26 |
missing_cols = required_columns - set(df.columns)
|
| 27 |
return ["All"], ["All"], ["All"], f"CSV is missing required columns: {', '.join(missing_cols)}", "All", "All", "All"
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Extract unique values for dropdowns
|
| 33 |
labs = ['All'] + sorted([str(lab) for lab in df['Lab'].dropna().unique() if str(lab).strip()])
|
|
@@ -47,6 +59,9 @@ def filter_and_visualize(selected_lab, selected_type, selected_date_range):
|
|
| 47 |
if df.empty:
|
| 48 |
return None, None, None, None, "No data available."
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
# Filter the DataFrame
|
| 51 |
filtered_df = df.copy()
|
| 52 |
if selected_lab != "All":
|
|
@@ -62,7 +77,10 @@ def filter_and_visualize(selected_lab, selected_type, selected_date_range):
|
|
| 62 |
if filtered_df.empty:
|
| 63 |
return None, None, None, None, "No data matches the selected filters."
|
| 64 |
|
| 65 |
-
#
|
|
|
|
|
|
|
|
|
|
| 66 |
device_cards = filtered_df[['DeviceID', 'Lab', 'Type', 'UsageCount', 'Timestamp']].sort_values(by='Timestamp', ascending=False)
|
| 67 |
|
| 68 |
# Daily Log Trends (Line Chart)
|
|
@@ -128,7 +146,6 @@ def download_pdf(selected_lab, selected_type, selected_date_range):
|
|
| 128 |
pdf.cell(200, 10, txt="LabOps Dashboard Report", ln=True, align='C')
|
| 129 |
pdf.ln(10)
|
| 130 |
|
| 131 |
-
# Add filtered data (removed 'Health')
|
| 132 |
for index, row in filtered_df.iterrows():
|
| 133 |
line = f"{row['Timestamp']} | {row['DeviceID']} | {row['Lab']} | {row['Type']} | {row['Status']} | {row['UsageCount']}"
|
| 134 |
pdf.multi_cell(0, 10, txt=line)
|
|
|
|
| 20 |
if df.empty:
|
| 21 |
return ["All"], ["All"], ["All"], "The uploaded CSV file is empty.", "All", "All", "All"
|
| 22 |
|
| 23 |
+
# Define required columns
|
| 24 |
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
| 25 |
if not required_columns.issubset(df.columns):
|
| 26 |
missing_cols = required_columns - set(df.columns)
|
| 27 |
return ["All"], ["All"], ["All"], f"CSV is missing required columns: {', '.join(missing_cols)}", "All", "All", "All"
|
| 28 |
|
| 29 |
+
# Debug: Print the DataFrame to check its content
|
| 30 |
+
print("DataFrame content:\n", df.to_string())
|
| 31 |
+
|
| 32 |
+
# Check for empty or all-NaN columns
|
| 33 |
+
if df['Lab'].dropna().empty or df['Type'].dropna().empty:
|
| 34 |
+
return ["All"], ["All"], ["All"], "Lab or Type columns are empty or contain only NaN values.", "All", "All", "All"
|
| 35 |
+
|
| 36 |
+
# Convert Timestamp to datetime with error handling
|
| 37 |
+
try:
|
| 38 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')
|
| 39 |
+
if df['Timestamp'].isna().all():
|
| 40 |
+
return ["All"], ["All"], ["All"], "All Timestamp values are invalid or unparseable.", "All", "All", "All"
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return ["All"], ["All"], ["All"], f"Failed to parse Timestamp column: {str(e)}", "All", "All", "All"
|
| 43 |
|
| 44 |
# Extract unique values for dropdowns
|
| 45 |
labs = ['All'] + sorted([str(lab) for lab in df['Lab'].dropna().unique() if str(lab).strip()])
|
|
|
|
| 59 |
if df.empty:
|
| 60 |
return None, None, None, None, "No data available."
|
| 61 |
|
| 62 |
+
# Debug: Print the DataFrame before filtering
|
| 63 |
+
print("DataFrame before filtering:\n", df.to_string())
|
| 64 |
+
|
| 65 |
# Filter the DataFrame
|
| 66 |
filtered_df = df.copy()
|
| 67 |
if selected_lab != "All":
|
|
|
|
| 77 |
if filtered_df.empty:
|
| 78 |
return None, None, None, None, "No data matches the selected filters."
|
| 79 |
|
| 80 |
+
# Debug: Print the filtered DataFrame
|
| 81 |
+
print("Filtered DataFrame:\n", filtered_df.to_string())
|
| 82 |
+
|
| 83 |
+
# Device Cards (as a table)
|
| 84 |
device_cards = filtered_df[['DeviceID', 'Lab', 'Type', 'UsageCount', 'Timestamp']].sort_values(by='Timestamp', ascending=False)
|
| 85 |
|
| 86 |
# Daily Log Trends (Line Chart)
|
|
|
|
| 146 |
pdf.cell(200, 10, txt="LabOps Dashboard Report", ln=True, align='C')
|
| 147 |
pdf.ln(10)
|
| 148 |
|
|
|
|
| 149 |
for index, row in filtered_df.iterrows():
|
| 150 |
line = f"{row['Timestamp']} | {row['DeviceID']} | {row['Lab']} | {row['Type']} | {row['Status']} | {row['UsageCount']}"
|
| 151 |
pdf.multi_cell(0, 10, txt=line)
|