-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbatch_render.py
More file actions
89 lines (68 loc) · 2.9 KB
/
batch_render.py
File metadata and controls
89 lines (68 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
"""
Batch Email Renderer
===================
This script demonstrates batch rendering of personalized emails
for multiple users using the EmailRenderer class.
Usage:
python batch_render.py
"""
import os
from pathlib import Path
from email_renderer import EmailRenderer
def batch_render_emails():
"""Render emails for all sample users."""
renderer = EmailRenderer()
base_dir = Path(__file__).parent
templates_dir = base_dir / "templates"
# Find all user data files
user_files = list(templates_dir.glob("user_*_data.json"))
if not user_files:
print("❌ No user data files found. Run with --create-samples first.")
return
print(f"🚀 Starting batch rendering for {len(user_files)} users...\n")
rendered_files = []
for user_file in user_files:
try:
# Extract user identifier from filename
user_id = user_file.stem.replace("_data", "")
output_file = f"batch_rendered_{user_id}.html"
print(f"👤 Rendering email for {user_file.name}...")
# Render personalized email
output_path = renderer.render_email(
user_data_file=str(user_file),
output_file=output_file
)
rendered_files.append(output_path)
print(f"✅ Saved: {output_path}\n")
except Exception as e:
print(f"❌ Error rendering {user_file}: {e}\n")
print(f"🎉 Batch rendering complete!")
print(f"📧 Rendered {len(rendered_files)} emails:")
for file_path in rendered_files:
print(f" • {file_path}")
def demo_advanced_features():
"""Demonstrate advanced features of the email renderer."""
print("🎨 Advanced Email Rendering Demo\n")
renderer = EmailRenderer()
# Load base data and show customization
data = renderer.load_data()
print("📊 Template Data Structure:")
print(f" • Company: {data['company']['name']}")
print(f" • Newsletter: {data['newsletter']['title']} ({data['newsletter']['date']})")
print(f" • Articles: {len(data['articles'])} featured articles")
print(f" • Feature: {data['feature']['title']}")
print(f" • Promo Code: {data['promo']['code']}")
print()
# Show user personalization
user_files = list(Path("templates").glob("user_*_data.json"))
for user_file in user_files[:2]: # Show first 2 users
user_data = renderer.load_user_data(str(user_file))
print(f"👤 User: {user_data.get('name', 'Unknown')}")
print(f" • Email: {user_data.get('email', 'N/A')}")
print(f" • Plan: {user_data.get('plan', 'N/A')}")
print(f" • Interests: {', '.join(user_data.get('interests', []))}")
print()
if __name__ == "__main__":
demo_advanced_features()
batch_render_emails()