import sys
import json
from pathlib import Path
# Add project root to sys.path to allow importing tradingagents
current_dir = Path(__file__).parent
sys.path.append(str(current_dir.parent))
try:
from tradingagents.utils.anonymizer import TickerAnonymizer
ANONYMIZER_AVAILABLE = True
except ImportError:
ANONYMIZER_AVAILABLE = False
TEMPLATE = """
Trading Agent Report - {ticker} - {date}
"""
def generate_report(report_dir):
path = Path(report_dir)
if not path.exists():
print(f"Error: Directory {report_dir} not found.")
return
# Extract info from path structure: results/TICKER/DATE/reports
try:
data_parts = path.parts
# Assuming structure .../TICKER/DATE/reports
date = data_parts[-2]
ticker = data_parts[-3]
except IndexError:
date = "Unknown Date"
ticker = "Unknown Ticker"
anonymizer = None
if ANONYMIZER_AVAILABLE:
anonymizer = TickerAnonymizer()
real_ticker = anonymizer.deanonymize_ticker(ticker)
if real_ticker:
ticker = f"{real_ticker} ({ticker})"
reports = {}
# Read all markdown files
for file in path.glob("*.md"):
try:
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
# Deanonymize content if possible
if anonymizer:
content = anonymizer.deanonymize_text(content)
reports[file.name] = content
except Exception as e:
print(f"Failed to read {file}: {e}")
if not reports:
print("No markdown files found to generate report.")
return
# Sort keys to ensure consistent order (e.g. Investment Plan first if possible, or alphabetical)
# Let's prioritize investment_plan.md
sorted_keys = sorted(reports.keys(), key=lambda x: (0 if "plan" in x else 1, x))
sorted_reports = {k: reports[k] for k in sorted_keys}
# Generate HTML
html_content = TEMPLATE.replace("{ticker}", ticker).replace("{date}", date)
html_content = html_content.replace("{json_data}", json.dumps(sorted_reports))
output_path = path / "index.html"
with open(output_path, 'w', encoding='utf-8') as f:
f.write(html_content)
print(f"✅ Generated Dashboard: {output_path}")
return str(output_path)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 generate_report_html.py ")
sys.exit(1)
generate_report(sys.argv[1])