RathodHarish commited on
Commit
3ac903b
·
verified ·
1 Parent(s): b2acd65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
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 fpdf import FPDF
 
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 to PDF
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
- pdf.cell(200, 10, txt="LabOps Dashboard Summary Report", ln=True, align='C')
61
- pdf.cell(200, 10, txt=f"Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
62
- pdf.ln(10)
63
 
64
- headers = ["Lab", "Device", "OK", "DOWN"]
65
- col_widths = [40, 40, 30, 30]
66
- for header, width in zip(headers, col_widths):
67
- pdf.cell(width, 10, header, border=1)
68
- pdf.ln()
 
 
 
 
 
 
 
 
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.cell(40, 10, lab, border=1)
74
- pdf.cell(40, 10, dev_type, border=1)
75
- pdf.cell(30, 10, str(ok), border=1)
76
- pdf.cell(30, 10, str(down), border=1)
77
- pdf.ln()
78
-
79
- # Convert to byte stream for Gradio
80
- pdf_output = pdf.output(dest='S').encode('latin1')
81
- return ("LabOps_Summary.pdf", pdf_output)
 
 
 
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