Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,8 @@ import pandas as pd
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import io
|
| 5 |
from datetime import datetime
|
| 6 |
-
from
|
|
|
|
| 7 |
|
| 8 |
# ----------------------------
|
| 9 |
# Load Logs
|
|
@@ -13,7 +14,6 @@ def load_logs(file_obj):
|
|
| 13 |
content = file_obj.read().decode('utf-8')
|
| 14 |
df = pd.read_csv(io.StringIO(content), parse_dates=['Timestamp'])
|
| 15 |
else:
|
| 16 |
-
# Sample fallback data
|
| 17 |
sample_data = {
|
| 18 |
'DeviceID': ['D001', 'D002', 'D003'],
|
| 19 |
'Lab': ['Lab A', 'Lab B', 'Lab A'],
|
|
@@ -49,36 +49,44 @@ def generate_chart(df):
|
|
| 49 |
return fig
|
| 50 |
|
| 51 |
# ----------------------------
|
| 52 |
-
# Export
|
| 53 |
# ----------------------------
|
| 54 |
def export_pdf(df):
|
| 55 |
summary = summarize_logs(df)
|
| 56 |
-
pdf = FPDF()
|
| 57 |
-
pdf.add_page()
|
| 58 |
-
pdf.set_font("Arial", size=12)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
pdf
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
for (lab, dev_type), row in summary.iterrows():
|
| 71 |
ok = int(row.get('OK', 0))
|
| 72 |
down = int(row.get('DOWN', 0))
|
| 73 |
-
pdf.
|
| 74 |
-
pdf.
|
| 75 |
-
pdf.
|
| 76 |
-
pdf.
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# ----------------------------
|
| 84 |
# Gradio App
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import io
|
| 5 |
from datetime import datetime
|
| 6 |
+
from reportlab.lib.pagesizes import letter
|
| 7 |
+
from reportlab.pdfgen import canvas
|
| 8 |
|
| 9 |
# ----------------------------
|
| 10 |
# Load Logs
|
|
|
|
| 14 |
content = file_obj.read().decode('utf-8')
|
| 15 |
df = pd.read_csv(io.StringIO(content), parse_dates=['Timestamp'])
|
| 16 |
else:
|
|
|
|
| 17 |
sample_data = {
|
| 18 |
'DeviceID': ['D001', 'D002', 'D003'],
|
| 19 |
'Lab': ['Lab A', 'Lab B', 'Lab A'],
|
|
|
|
| 49 |
return fig
|
| 50 |
|
| 51 |
# ----------------------------
|
| 52 |
+
# Export PDF with ReportLab
|
| 53 |
# ----------------------------
|
| 54 |
def export_pdf(df):
|
| 55 |
summary = summarize_logs(df)
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
buffer = io.BytesIO()
|
| 58 |
+
pdf = canvas.Canvas(buffer, pagesize=letter)
|
| 59 |
+
width, height = letter
|
| 60 |
|
| 61 |
+
pdf.setFont("Helvetica-Bold", 14)
|
| 62 |
+
pdf.drawCentredString(width / 2, height - 40, "LabOps Dashboard Summary Report")
|
| 63 |
+
pdf.setFont("Helvetica", 10)
|
| 64 |
+
pdf.drawCentredString(width / 2, height - 60, f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
| 65 |
+
|
| 66 |
+
y = height - 100
|
| 67 |
+
pdf.setFont("Helvetica-Bold", 10)
|
| 68 |
+
pdf.drawString(50, y, "Lab")
|
| 69 |
+
pdf.drawString(150, y, "Device Type")
|
| 70 |
+
pdf.drawString(300, y, "OK")
|
| 71 |
+
pdf.drawString(350, y, "DOWN")
|
| 72 |
+
y -= 20
|
| 73 |
+
pdf.setFont("Helvetica", 10)
|
| 74 |
|
| 75 |
for (lab, dev_type), row in summary.iterrows():
|
| 76 |
ok = int(row.get('OK', 0))
|
| 77 |
down = int(row.get('DOWN', 0))
|
| 78 |
+
pdf.drawString(50, y, str(lab))
|
| 79 |
+
pdf.drawString(150, y, str(dev_type))
|
| 80 |
+
pdf.drawString(300, y, str(ok))
|
| 81 |
+
pdf.drawString(350, y, str(down))
|
| 82 |
+
y -= 20
|
| 83 |
+
if y < 50:
|
| 84 |
+
pdf.showPage()
|
| 85 |
+
y = height - 50
|
| 86 |
+
|
| 87 |
+
pdf.save()
|
| 88 |
+
buffer.seek(0)
|
| 89 |
+
return ("LabOps_Summary.pdf", buffer.read())
|
| 90 |
|
| 91 |
# ----------------------------
|
| 92 |
# Gradio App
|