Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,69 +4,112 @@ import matplotlib.pyplot as plt
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
import io
|
| 6 |
|
|
|
|
| 7 |
df = pd.DataFrame()
|
| 8 |
|
| 9 |
def upload_csv(file):
|
| 10 |
global df
|
| 11 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
df = pd.read_csv(file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
|
|
|
|
|
|
| 14 |
if not required_columns.issubset(df.columns):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
labs = ['All'] + sorted(df['Lab'].dropna().unique().tolist())
|
| 17 |
types = ['All'] + sorted(df['Type'].dropna().unique().tolist())
|
| 18 |
-
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
def filter_and_plot(selected_lab, selected_type):
|
| 23 |
global df
|
|
|
|
| 24 |
if df.empty:
|
| 25 |
return None
|
|
|
|
|
|
|
| 26 |
filtered_df = df.copy()
|
|
|
|
|
|
|
| 27 |
if selected_lab != "All":
|
| 28 |
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 29 |
if selected_type != "All":
|
| 30 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 31 |
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
plt.figure(figsize=(8, 4))
|
| 34 |
status_counts = filtered_df["Status"].value_counts()
|
| 35 |
status_counts.plot(kind="bar", color=["green", "red"])
|
| 36 |
-
plt.title("
|
| 37 |
plt.xlabel("Status")
|
| 38 |
plt.ylabel("Count")
|
| 39 |
|
|
|
|
| 40 |
buf = io.BytesIO()
|
| 41 |
plt.savefig(buf, format="png")
|
|
|
|
| 42 |
buf.seek(0)
|
| 43 |
return buf
|
| 44 |
|
| 45 |
def download_pdf(selected_lab, selected_type):
|
| 46 |
global df
|
|
|
|
| 47 |
if df.empty:
|
| 48 |
return None
|
|
|
|
|
|
|
| 49 |
filtered_df = df.copy()
|
|
|
|
|
|
|
| 50 |
if selected_lab != "All":
|
| 51 |
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 52 |
if selected_type != "All":
|
| 53 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
pdf = FPDF()
|
| 56 |
pdf.add_page()
|
| 57 |
pdf.set_font("Arial", size=12)
|
| 58 |
pdf.cell(200, 10, txt="LabOps Summary Report", ln=True, align='C')
|
| 59 |
pdf.ln(10)
|
| 60 |
-
|
|
|
|
| 61 |
for index, row in filtered_df.iterrows():
|
| 62 |
line = f"{row['Timestamp']} | {row['DeviceID']} | {row['Lab']} | {row['Type']} | {row['Status']} | {row['UsageCount']}"
|
| 63 |
pdf.multi_cell(0, 10, txt=line)
|
| 64 |
-
|
|
|
|
| 65 |
output = io.BytesIO()
|
| 66 |
pdf.output(output)
|
| 67 |
output.seek(0)
|
| 68 |
return output
|
| 69 |
|
|
|
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown("🧪 **LabOps Dashboard with Filters**\nUpload lab device logs, filter by Lab and Equipment Type, visualize uptime/downtime & generate PDF reports.")
|
| 72 |
|
|
@@ -77,15 +120,14 @@ with gr.Blocks() as demo:
|
|
| 77 |
lab_dropdown = gr.Dropdown(label="Select Lab", choices=["All"], value="All")
|
| 78 |
type_dropdown = gr.Dropdown(label="Select Equipment Type", choices=["All"], value="All")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
plot_output = gr.Image(label="Device Status Plot")
|
| 83 |
download_btn = gr.Button("Download PDF Summary")
|
|
|
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
csv_input.change(fn=upload_csv, inputs=csv_input, outputs=[lab_dropdown, type_dropdown, error_box])
|
| 87 |
lab_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 88 |
type_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 89 |
-
download_btn.click(fn=download_pdf, inputs=[lab_dropdown, type_dropdown], outputs=gr.File())
|
| 90 |
|
| 91 |
-
demo.launch()
|
|
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
# Global DataFrame to store the CSV data
|
| 8 |
df = pd.DataFrame()
|
| 9 |
|
| 10 |
def upload_csv(file):
|
| 11 |
global df
|
| 12 |
try:
|
| 13 |
+
# Check if a file was actually uploaded
|
| 14 |
+
if file is None:
|
| 15 |
+
return (["All"], ["All"]), "No file uploaded. Please upload a CSV file."
|
| 16 |
+
|
| 17 |
+
# Read the CSV file
|
| 18 |
df = pd.read_csv(file.name)
|
| 19 |
+
|
| 20 |
+
# Check if the DataFrame is empty
|
| 21 |
+
if df.empty:
|
| 22 |
+
return (["All"], ["All"]), "The uploaded CSV file is empty."
|
| 23 |
+
|
| 24 |
+
# Define required columns
|
| 25 |
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
| 26 |
+
|
| 27 |
+
# Check if all required columns are present
|
| 28 |
if not required_columns.issubset(df.columns):
|
| 29 |
+
missing_cols = required_columns - set(df.columns)
|
| 30 |
+
return (["All"], ["All"]), f"CSV is missing required columns: {', '.join(missing_cols)}"
|
| 31 |
+
|
| 32 |
+
# Extract unique values for dropdowns
|
| 33 |
labs = ['All'] + sorted(df['Lab'].dropna().unique().tolist())
|
| 34 |
types = ['All'] + sorted(df['Type'].dropna().unique().tolist())
|
| 35 |
+
|
| 36 |
+
return (labs, types), ""
|
| 37 |
except Exception as e:
|
| 38 |
+
# Handle any other errors (e.g., file not readable, invalid CSV format)
|
| 39 |
+
return (["All"], ["All"]), f"Failed to load CSV: {str(e)}"
|
| 40 |
|
| 41 |
def filter_and_plot(selected_lab, selected_type):
|
| 42 |
global df
|
| 43 |
+
# If no data is loaded, return None (no plot)
|
| 44 |
if df.empty:
|
| 45 |
return None
|
| 46 |
+
|
| 47 |
+
# Create a copy of the DataFrame for filtering
|
| 48 |
filtered_df = df.copy()
|
| 49 |
+
|
| 50 |
+
# Apply filters based on dropdown selections
|
| 51 |
if selected_lab != "All":
|
| 52 |
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 53 |
if selected_type != "All":
|
| 54 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 55 |
|
| 56 |
+
# If no data remains after filtering, return None
|
| 57 |
+
if filtered_df.empty:
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
# Prepare the plot
|
| 61 |
plt.figure(figsize=(8, 4))
|
| 62 |
status_counts = filtered_df["Status"].value_counts()
|
| 63 |
status_counts.plot(kind="bar", color=["green", "red"])
|
| 64 |
+
plt.title("Status Counts")
|
| 65 |
plt.xlabel("Status")
|
| 66 |
plt.ylabel("Count")
|
| 67 |
|
| 68 |
+
# Save the plot to a buffer
|
| 69 |
buf = io.BytesIO()
|
| 70 |
plt.savefig(buf, format="png")
|
| 71 |
+
plt.close() # Close the plot to free memory
|
| 72 |
buf.seek(0)
|
| 73 |
return buf
|
| 74 |
|
| 75 |
def download_pdf(selected_lab, selected_type):
|
| 76 |
global df
|
| 77 |
+
# If no data is loaded, return None (no PDF)
|
| 78 |
if df.empty:
|
| 79 |
return None
|
| 80 |
+
|
| 81 |
+
# Create a copy of the DataFrame for filtering
|
| 82 |
filtered_df = df.copy()
|
| 83 |
+
|
| 84 |
+
# Apply filters based on dropdown selections
|
| 85 |
if selected_lab != "All":
|
| 86 |
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 87 |
if selected_type != "All":
|
| 88 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 89 |
+
|
| 90 |
+
# If no data remains after filtering, return None
|
| 91 |
+
if filtered_df.empty:
|
| 92 |
+
return None
|
| 93 |
+
|
| 94 |
+
# Generate the PDF
|
| 95 |
pdf = FPDF()
|
| 96 |
pdf.add_page()
|
| 97 |
pdf.set_font("Arial", size=12)
|
| 98 |
pdf.cell(200, 10, txt="LabOps Summary Report", ln=True, align='C')
|
| 99 |
pdf.ln(10)
|
| 100 |
+
|
| 101 |
+
# Add data to the PDF
|
| 102 |
for index, row in filtered_df.iterrows():
|
| 103 |
line = f"{row['Timestamp']} | {row['DeviceID']} | {row['Lab']} | {row['Type']} | {row['Status']} | {row['UsageCount']}"
|
| 104 |
pdf.multi_cell(0, 10, txt=line)
|
| 105 |
+
|
| 106 |
+
# Save the PDF to a buffer
|
| 107 |
output = io.BytesIO()
|
| 108 |
pdf.output(output)
|
| 109 |
output.seek(0)
|
| 110 |
return output
|
| 111 |
|
| 112 |
+
# Build the Gradio interface
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
gr.Markdown("🧪 **LabOps Dashboard with Filters**\nUpload lab device logs, filter by Lab and Equipment Type, visualize uptime/downtime & generate PDF reports.")
|
| 115 |
|
|
|
|
| 120 |
lab_dropdown = gr.Dropdown(label="Select Lab", choices=["All"], value="All")
|
| 121 |
type_dropdown = gr.Dropdown(label="Select Equipment Type", choices=["All"], value="All")
|
| 122 |
|
| 123 |
+
plot_output = gr.Image(label="Plot")
|
|
|
|
|
|
|
| 124 |
download_btn = gr.Button("Download PDF Summary")
|
| 125 |
+
error_box = gr.Textbox(label="Status/Error Message", visible=True, interactive=False)
|
| 126 |
|
| 127 |
+
# Connect the components
|
| 128 |
+
csv_input.change(fn=upload_csv, inputs=csv_input, outputs=[(lab_dropdown, type_dropdown), error_box])
|
| 129 |
lab_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 130 |
type_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 131 |
+
download_btn.click(fn=download_pdf, inputs=[lab_dropdown, type_dropdown], outputs=gr.File(label="Download PDF"))
|
| 132 |
|
| 133 |
+
demo.launch()
|