""" Example Usage of Report Generation API ====================================== This file demonstrates how to use the complete report generation system for both PDF-style reports and PPT-style presentations. """ import requests import json from typing import Dict, Any # API Base URL (update with your server address) API_BASE_URL = "https://mr-mvp-api-dev.dev.ingenspark.com//report_generation" # ============================================================================ # EXAMPLE 1: Generate HTML Report (PDF-style multi-page document) # ============================================================================ def generate_html_report_example(): """Generate a professional HTML report.""" # Sample context data context_data = { "title": "Quarterly Sales Analysis", "period": "Q4 2024", "metrics": { "total_revenue": 1500000, "growth_rate": 15.5, "customer_count": 2500, "avg_order_value": 600 }, "regional_sales": [ {"region": "North", "sales": 450000}, {"region": "South", "sales": 380000}, {"region": "East", "sales": 420000}, {"region": "West", "sales": 250000} ] } payload = { "format_type": "html", "output_type": "report", # PDF-style report "reportname": "Q4 2024 Sales Report", "include_citations": True, "success": True, "list_of_queries": ["sales analysis", "revenue trends"], "theme": "professional", "font_family": "Times New Roman", "branding": "ACME Corporation", "pageoptions": { "include_cover_page": True, "include_header_footer": True, "include_toc": True, "num_pages": 9 }, "context_data": context_data, "use_advanced_generator": True } response = requests.post(f"{API_BASE_URL}/generate_html", json=payload) if response.status_code == 200: result = response.json() print("✅ HTML Report Generated!") print(f"Status: {result['report_generation_status']}") print(f"File saved: {result.get('file_saved_path')}") # Save HTML to file with open("sales_report.html", "w", encoding="utf-8") as f: f.write(result['html_content']) return result else: print(f"❌ Error: {response.status_code}") print(response.text) # ============================================================================ # EXAMPLE 2: Generate HTML Presentation (PPT-style slides) # ============================================================================ def generate_html_presentation_example(): """Generate a presentation-style HTML output.""" context_data = { "title": "State of Remote Work 2024", "survey_data": { "total_respondents": 1000, "remote_preference": { "fully_remote": 45, "hybrid": 40, "office": 15 }, "productivity_rating": 4.2, "stress_levels": { "low": 30, "medium": 50, "high": 20 } }, "key_insights": [ "Remote work increases productivity by 15%", "Employees save average 2 hours daily on commute", "90% want flexible work options to continue" ], "quotes": [ {"author": "John Doe", "text": "Remote work changed my life"}, {"author": "Jane Smith", "text": "Best work-life balance ever"} ] } payload = { "format_type": "html", "output_type": "presentation", # PPT-style presentation "reportname": "State of Remote Work 2024", "include_citations": False, "success": True, "theme": "modern", "font_family": "Roboto", "branding": "FUTURE WORK INSIGHTS", "color_scheme": { "primary": "#191970", "secondary": "#483D8B", "accent": "#3CB371" }, "pageoptions": { "include_cover_page": True, "include_header_footer": True, "num_slides": 11 }, "context_data": context_data, "use_advanced_generator": True } response = requests.post(f"{API_BASE_URL}/generate_html", json=payload) if response.status_code == 200: result = response.json() print("✅ HTML Presentation Generated!") print(f"Status: {result['report_generation_status']}") print(f"File saved: {result.get('file_saved_path')}") # Save to file with open("remote_work_presentation.html", "w", encoding="utf-8") as f: f.write(result['html_content']) return result else: print(f"❌ Error: {response.status_code}") # ============================================================================ # EXAMPLE 3: Generate PDF Report # ============================================================================ def generate_pdf_report_example(): """Generate a PDF report directly.""" payload = { "format_type": "pdf", "output_type": "report", "reportname": "Annual Financial Report 2024", "theme": "corporate", "pageoptions": { "include_cover_page": True, "include_toc": True, "num_pages": 15 }, "context_file_path": "context.txt", # Load from file "use_advanced_generator": True } response = requests.post(f"{API_BASE_URL}/generate_pdf", json=payload) if response.status_code == 200: result = response.json() print("✅ PDF Report Generated!") # Decode and save PDF if result.get('pdf_base64'): import base64 pdf_data = base64.b64decode(result['pdf_base64']) with open("financial_report.pdf", "wb") as f: f.write(pdf_data) print("📄 PDF saved as financial_report.pdf") return result # ============================================================================ # EXAMPLE 4: Generate PDF Presentation # ============================================================================ def generate_pdf_presentation_example(): """Generate a PDF presentation.""" context_data = { "title": "Product Launch Strategy", "launch_date": "Q1 2025", "target_audience": ["Tech enthusiasts", "Early adopters", "Businesses"], "marketing_channels": [ {"channel": "Social Media", "budget": 50000, "reach": 500000}, {"channel": "Email", "budget": 20000, "reach": 100000}, {"channel": "Events", "budget": 80000, "reach": 50000} ] } payload = { "format_type": "pdf", "output_type": "presentation", "reportname": "Product Launch Strategy 2025", "theme": "creative", "branding": "Innovation Labs", "pageoptions": { "num_slides": 15 }, "context_data": context_data, "use_advanced_generator": True } response = requests.post(f"{API_BASE_URL}/generate_pdf", json=payload) if response.status_code == 200: result = response.json() print("✅ PDF Presentation Generated!") return result # ============================================================================ # EXAMPLE 5: Save Existing HTML as PDF # ============================================================================ def save_html_as_pdf_example(html_content: str): """Convert existing HTML to PDF.""" payload = { "format_type": "pdf", "output_type": "report", "reportname": "Converted Report", "html_content": html_content, "pdf_options": { "format": "A4", "margin": { "top": "20mm", "bottom": "20mm", "left": "20mm", "right": "20mm" } } } response = requests.post(f"{API_BASE_URL}/save_generated_report", json=payload) if response.status_code == 200: result = response.json() print("✅ HTML converted to PDF!") return result # ============================================================================ # EXAMPLE 6: Check API Health # ============================================================================ def check_health(): """Check if the API is healthy.""" response = requests.get(f"{API_BASE_URL}/health") if response.status_code == 200: health = response.json() print("✅ API is healthy") print(f"Service: {health['service']}") print(f"Supported outputs: {health['supported_outputs']}") return health # ============================================================================ # EXAMPLE 7: List Available Themes # ============================================================================ def list_themes(): """Get list of available themes.""" response = requests.get(f"{API_BASE_URL}/themes") if response.status_code == 200: themes = response.json() print("📋 Available Themes:") for theme in themes['themes']: print(f" - {theme}") return themes # ============================================================================ # Run Examples # ============================================================================ if __name__ == "__main__": print("=" * 70) print("REPORT GENERATION API - USAGE EXAMPLES") print("=" * 70) # Check health print("\n1. Checking API Health...") # check_health() # List themes print("\n2. Listing Available Themes...") # list_themes() # Generate HTML report print("\n3. Generating HTML Report...") generate_html_report_example() # Generate HTML presentation print("\n4. Generating HTML Presentation...") # generate_html_presentation_example() # Generate PDF report print("\n5. Generating PDF Report...") # generate_pdf_report_example() # Generate PDF presentation print("\n6. Generating PDF Presentation...") # generate_pdf_presentation_example() print("\n" + "=" * 70) print("✅ All examples completed!") print("=" * 70)