function reagents_report
Generates a PDF report for reagents audit log data, including title, requester information, date, and a table of the provided dataframe.
/tf/active/vicechatdev/resources/reports.py
123 - 130
moderate
Purpose
This function creates a formatted PDF document containing an audit log report for reagents. It takes a pandas DataFrame with reagents data and a username, then generates a PDF with a header section (title, requester, date) and a multi-cell table displaying the dataframe contents. The PDF is returned as a BytesIO object for further processing or download.
Source Code
def reagents_report(df, user):
pdf = PDF()
pdf.add_page()
pdf.titles([f"Title: Audit Log: Reagents", f"Requested by: {user}", f"Date: {dt.datetime.now().strftime('%Y-%m-%d')}"])
pdf.multi_cell_table(df)
pdf_bytes = io.BytesIO(pdf.output(dest='S').encode('latin-1'))
pdf_bytes.seek(0)
return pdf_bytes
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
df |
- | - | positional_or_keyword |
user |
- | - | positional_or_keyword |
Parameter Details
df: A pandas DataFrame containing the reagents audit log data to be displayed in the PDF table. Expected to have columns and rows that will be rendered in a tabular format.
user: A string representing the username or identifier of the person requesting the report. This will be displayed in the PDF header as 'Requested by: {user}'.
Return Value
Returns an io.BytesIO object containing the generated PDF file encoded in 'latin-1' format. The BytesIO object's position is reset to the beginning (seek(0)) so it's ready to be read or saved. This can be used to save the PDF to disk, send as an email attachment, or return as an HTTP response.
Dependencies
pandasdatetimeiofpdfconfig
Required Imports
import pandas as pd
import datetime as dt
import io
from fpdf import FPDF
import config
Usage Example
import pandas as pd
import datetime as dt
import io
from fpdf import FPDF
# Assuming PDF class is defined with required methods
class PDF(FPDF):
def titles(self, title_list):
for title in title_list:
self.cell(0, 10, title, ln=True)
def multi_cell_table(self, df):
# Implementation to render dataframe as table
pass
# Create sample dataframe
df = pd.DataFrame({
'Reagent': ['Reagent A', 'Reagent B'],
'Quantity': [100, 200],
'Date': ['2023-01-01', '2023-01-02']
})
# Generate report
pdf_bytes = reagents_report(df, 'john.doe@example.com')
# Save to file
with open('reagents_report.pdf', 'wb') as f:
f.write(pdf_bytes.read())
Best Practices
- Ensure the PDF class is properly defined with 'titles()' and 'multi_cell_table()' methods before calling this function
- The dataframe should be properly formatted and not contain data that would cause rendering issues in the PDF
- Consider validating that the dataframe is not empty before generating the report
- The 'latin-1' encoding may cause issues with special characters; consider using UTF-8 encoding if the FPDF version supports it
- The returned BytesIO object should be properly closed after use to free memory
- Consider adding error handling for PDF generation failures
- The user parameter should be sanitized if it comes from untrusted sources to prevent injection issues
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class PDF 62.2% similar
-
function generate_report 55.9% similar
-
class AuditPageGenerator 54.2% similar
-
class ConversationTimelineGenerator 52.4% similar
-
function add_data_section_to_pdf 52.3% similar