LinkedinMonitor / ui /insights_ui_generator.py
GuglielmoTor's picture
Update ui/insights_ui_generator.py
f67d3fd verified
import logging
from typing import Dict, Any, List, Optional
import pandas as pd
# Configure logger for this module. Assumes logging is configured in app.py or main entry point.
logger = logging.getLogger(__name__)
def format_report_for_display(report_data: Optional[pd.Series]) -> Dict[str, str]:
"""
Enhanced report formatting. Returns a dictionary with separate HTML for the header
and Markdown for the report body, allowing flexible rendering.
Args:
report_data: A pandas Series representing a single row from the agentic analysis DataFrame.
It should contain 'report_text', 'report_type', and 'Created Date'.
Returns:
A dictionary with 'header_html' and 'body_markdown' keys.
'header_html' contains the HTML for the report title and subtitle.
'body_markdown' contains the raw Markdown text for the report body.
If data is invalid, it returns empty state HTML for both.
"""
if report_data is None or report_data.empty:
empty_state_html = """
<div class="empty-state">
<div class="empty-state-icon">πŸ“„</div>
<div class="empty-state-title">Report Not Available</div>
<div class="empty-state-description">
The selected report could not be loaded. Please try selecting a different report
or refresh the page.
</div>
</div>
"""
return {'header_html': '', 'body_markdown': empty_state_html}
# Extract report data
# Ensure 'report_text' is treated as raw text that will be interpreted as Markdown
report_text = report_data.get('report_text', '*Report content not found.*')
report_type = report_data.get('report_type')
created_date_str = report_data.get('Created Date')
# Generate dynamic title and subtitle
title = "Comprehensive Analysis Report"
subtitle = ""
try:
if report_type == 'Quarter':
title = "πŸ“ˆ Quarterly Insights Report"
subtitle = "Strategic analysis of your quarterly performance"
elif report_type == 'Week' and pd.notna(created_date_str):
# Ensure pandas is used for datetime conversion
created_date = pd.to_datetime(created_date_str)
day_name = created_date.strftime('%A')
title = f"πŸ“Š {day_name}'s Weekly Update"
subtitle = f"Weekly performance analysis for {created_date.strftime('%B %d, %Y')}"
except Exception as e:
logger.error(f"Error generating dynamic report title: {e}")
# In case of an error, default title and no subtitle will be used.
# Format the report header HTML
header_html = f"""
<div class="report-header-content">
<h1>{title}</h1>
{f'<p style="font-size: 1.1rem; color: #6b6b6b; margin-bottom: 0;">{subtitle}</p>' if subtitle else ''}
</div>
"""
# The report_text itself should be the raw Markdown for the body
body_markdown = report_text.strip()
return {'header_html': header_html, 'body_markdown': body_markdown}
def format_report_to_markdown(report_string: Optional[str]) -> str:
"""
This function was previously for general Markdown formatting.
Given the new structure where 'format_report_for_display' handles the split,
this function might become redundant or repurposed.
Keeping it for now but noting its potential redundancy depending on upstream calls.
"""
if not report_string or not report_string.strip():
return "## Comprehensive Analysis Report\n\n*No analysis report was generated, or an error occurred during its generation.*"
# Simple formatting for now. Could be enhanced (e.g., looking for patterns like "Section X:" to make them H3)
formatted_report = f"## Comprehensive Analysis Report\n\n{report_string.strip()}"
return formatted_report
# REMOVED: extract_key_results_for_selection function
# REMOVED: format_single_okr_for_display function