This document provides detailed API documentation for the ReportGenerator class and related functions.
The main interface for generating environmental health disparity reports.
ReportGenerator(config: Optional[Dict] = None)
Parameters:
config (Dict, optional): Configuration dictionary. If None, uses default settings.Default Configuration:
{
'output_dir': 'output',
'geojson_path': 'inputs/geojson/ca_counties_simplified.geojson',
'population_csv_url': 'https://docs.google.com/spreadsheets/d/...',
'text_csv_url': 'https://docs.google.com/spreadsheets/d/...',
'census_tracts_geojson': 'inputs/geojson/ca_census_tracts.geojson',
'heat_data_csv': 'inputs/heat_data.csv',
'air_pollution_csv': 'inputs/pm25_binned_data.csv',
'tract_level_data': 'inputs/tract_level_data.csv',
'roads_geojson': 'inputs/geojson/ca_primary_secondary_roads.geojson'
}
Load population and geographic data.
Parameters:
offline_mode (bool): If True, skip loading data from Google Sheets URLsReturns:
Example:
generator = ReportGenerator()
generator.load_data(offline_mode=False)
Generate population donut charts for specified counties.
Parameters:
counties (List[str], optional): List of county names. If None, generates for all counties.Returns:
Example:
generator.generate_population_charts(['Los Angeles', 'San Diego'])
Generate extreme heat vulnerability maps.
Parameters:
counties (List[str], optional): List of county names. If None, generates for all counties.Returns:
Example:
generator.generate_heat_maps(['Los Angeles'])
Generate air pollution exposure maps.
Parameters:
counties (List[str], optional): List of county names. If None, generates for all counties.Returns:
Example:
generator.generate_air_pollution_maps(['San Diego'])
Generate HTML reports using Flask templating.
Parameters:
report_type (str): Type of report (‘extremeheat’, ‘airpollution’, or ‘all’)counties (List[str], optional): List of county names. If None, generates for all counties.Returns:
Example:
generator.generate_html_reports('extremeheat', ['Los Angeles'])
Generate PDF reports from HTML.
Parameters:
report_type (str): Type of report (‘extremeheat’, ‘airpollution’, or ‘all’)counties (List[str], optional): List of county names. If None, generates for all counties.Returns:
Requirements:
Example:
generator.generate_pdfs('all', ['Los Angeles'])
Generate complete reports for specified counties.
Parameters:
counties (List[str], optional): List of county names. If None, generates for all available counties.include_pdfs (bool): Whether to generate PDF outputs (requires additional dependencies)offline_mode (bool): Whether to run without fetching online dataReturns:
Return Structure:
{
'counties_processed': ['Los Angeles', 'San Diego'],
'output_directory': 'output',
'files_generated': ['path/to/file1.html', 'path/to/file2.png', ...],
'error': 'error message if any' # Optional
}
Example:
results = generator.generate_full_report(
counties=['Los Angeles'],
include_pdfs=False,
offline_mode=True
)
print(f"Generated {len(results['files_generated'])} files")
Get list of available counties for report generation.
Returns:
Example:
counties = generator.list_available_counties()
print(f"Available counties: {counties}")
Get summary statistics for a specific county.
Parameters:
county_name (str): Name of the countyReturns:
Return Structure:
{
'county_name': 'Los Angeles',
'total_population': '10.0M',
'latino_population': '4.9M',
'nl_white_population': '2.7M',
'pct_latino': '49%',
'pct_nl_white': '27%'
}
Example:
summary = generator.get_county_summary('Los Angeles')
print(f"Total population: {summary['total_population']}")
Convenience function to generate reports for specific counties.
Parameters:
counties (List[str]): List of county namesoutput_dir (str): Output directory pathinclude_pdfs (bool): Whether to generate PDF outputsReturns:
Example:
from report_generator import generate_reports_for_counties
results = generate_reports_for_counties(
counties=['Los Angeles', 'San Diego'],
output_dir='my_reports',
include_pdfs=False
)
Generate a quick demo report for testing.
Parameters:
offline_mode (bool): Run without internet dependenciesReturns:
Example:
from report_generator import quick_demo
demo_results = quick_demo(offline_mode=True)
print(f"Demo generated {len(demo_results['files_generated'])} files")
The ReportGenerator class supports method chaining for efficient workflows:
results = (ReportGenerator()
.load_data(offline_mode=False)
.generate_population_charts(['Los Angeles'])
.generate_heat_maps(['Los Angeles'])
.generate_html_reports('extremeheat', ['Los Angeles'])
.generate_pdfs('extremeheat', ['Los Angeles']))
All methods include comprehensive error handling:
Common Error Patterns:
# Check for errors in results
results = generator.generate_full_report(['Invalid County'])
if 'error' in results:
print(f"Error occurred: {results['error']}")
# Validate county names
available = generator.list_available_counties()
requested = ['Los Angeles', 'Invalid County']
valid_counties = [c for c in requested if c in available]
config = {
'output_dir': '/path/to/custom/output',
'geojson_path': 'custom/path/to/counties.geojson'
}
generator = ReportGenerator(config=config)
config = {
'output_dir': '/var/www/reports',
'population_csv_url': 'https://your-custom-data-source.com/data.csv',
'census_tracts_geojson': '/data/census_tracts_2020.geojson'
}
generator = ReportGenerator(config=config)
config = {
'output_dir': 'test_output',
'geojson_path': 'test_data/sample_counties.geojson'
}
generator = ReportGenerator(config=config)
See requirements.txt for complete list:
For PDF generation:
Import Errors:
# Check dependencies
try:
from report_generator import ReportGenerator
print("✅ Import successful")
except ImportError as e:
print(f"❌ Import failed: {e}")
print("Run: pip install -r requirements.txt")
Data Loading Issues:
# Test data loading
generator = ReportGenerator()
try:
generator.load_data(offline_mode=False)
print("✅ Data loaded successfully")
except Exception as e:
print(f"❌ Data loading failed: {e}")
print("Try offline_mode=True for testing")
PDF Generation Issues:
# Check system dependencies
import shutil
if shutil.which('wkhtmltopdf'):
print("✅ wkhtmltopdf found")
else:
print("❌ wkhtmltopdf not found - install from https://wkhtmltopdf.org/")
if shutil.which('geckodriver'):
print("✅ geckodriver found")
else:
print("❌ geckodriver not found - download from Mozilla GitHub")
# Enable verbose logging
import logging
logging.basicConfig(level=logging.DEBUG)
# Check system diagnostics
generator = ReportGenerator()
print(f"Output directory: {generator.output_dir}")
print(f"Configuration: {generator.config}")
# Validate files exist
import os
required_files = [
'inputs/geojson/ca_counties_simplified.geojson',
'templates',
'static'
]
for file_path in required_files:
if os.path.exists(file_path):
print(f"✅ {file_path}")
else:
print(f"❌ {file_path} - Missing required file")
For more information, see the main README.md and methodology documentation.