🔍 Code Extractor

function reagents_report

Maturity: 29

Generates a PDF report for reagents audit log data, including title, requester information, date, and a table of the provided dataframe.

File:
/tf/active/vicechatdev/resources/reports.py
Lines:
123 - 130
Complexity:
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

  • pandas
  • datetime
  • io
  • fpdf
  • config

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

    A custom PDF generation class that extends FPDF to create formatted PDF documents with titles, text, horizontal rules, and tables from pandas DataFrames.

    From: /tf/active/vicechatdev/resources/reports.py
  • function generate_report 55.9% similar

    Generates a text-based statistical analysis report from session data and saves it to the configured reports folder.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • class AuditPageGenerator 54.2% similar

    A class that generates comprehensive PDF audit trail pages for documents, including document information, reviews, approvals, revision history, and event history with electronic signatures.

    From: /tf/active/vicechatdev/document_auditor/src/audit_page_generator.py
  • class ConversationTimelineGenerator 52.4% similar

    A class that generates comprehensive PDF reports documenting conversation timelines, including detailed exchanges, problem-solving analysis, references, and visual summaries.

    From: /tf/active/vicechatdev/e-ink-llm/conversation_timeline.py
  • function add_data_section_to_pdf 52.3% similar

    Adds a data analysis section to a PDF document story, including analysis metadata, statistical conclusions, and embedded visualizations from saved content or analysis history.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse