📚 Basic Concepts¶
Before diving into EmailWidget, understanding its core concepts will help you build better email reports. This chapter introduces EmailWidget's design philosophy and key concepts.
📧 Email Class Overview¶
Basic Concepts¶
The Email
class is the core of EmailWidget, serving as the container and manager for all Widgets:
Python
from email_widget import Email, TextWidget
# 创建邮件对象
email = Email(title="报告标题")
# 设置元信息
email.set_subtitle("副标题")
email.set_footer("脚注信息")
# 管理Widget
email.add_widget(TextWidget('Hello, World!'))
email.remove_widget("Hello, World!")
email.clear_widgets()
# 导出结果
html_content = email.export_str()
file_path = email.export_html("report.html")
Lifecycle¶
The typical lifecycle of an Email object:
- Creation - Initialize email object
- Configuration - Set title, subtitle, footer, etc.
- Content Addition - Add various Widget components
- Rendering - Generate HTML content
- Export - Save to file or get string
Features¶
🧩 Widget Component System¶
Design Philosophy¶
All Widget components inherit from BaseWidget
, ensuring API consistency:
Python
from email_widget.core.base import BaseWidget
class MyCustomWidget(BaseWidget):
def __init__(self):
super().__init__()
self.widget_type = "custom"
def render(self) -> str:
# 渲染逻辑
return self._render_template("custom.html", context)
Common Features¶
All Widgets share the following common features:
Widget Categories¶
EmailWidget provides 12 professional components, categorized by function:
📝 Content Components¶
- TextWidget - Text content, 8 styles
- ImageWidget - Image display, multi-source support
- QuoteWidget - Quote style, author information
📊 Data Components¶
- TableWidget - Data tables, DataFrame integration
- ChartWidget - Chart display, matplotlib support
- LogWidget - Log display, level classification
📈 Metric Components¶
- ProgressWidget - Linear progress bar, 5 themes
- CircularProgressWidget - Circular progress, multiple sizes
- StatusWidget - Status management, dynamic updates
🎨 Interface Components¶
- AlertWidget - Alert reminders, 5 types
- CardWidget - Information cards, icon support
- ColumnWidget - Layout container, responsive design
🎯 Best Practices¶
Code Organization¶
Recommended code organization:
Python
# 1. 导入必要的库
from email_widget import Email, TextWidget, TableWidget, EmailConfig
from email_widget.core.enums import TextType
# 2. 数据准备
def prepare_data():
return {"sales": [100, 200, 300]}
# 3. 邮件构建
def build_email(data):
email = Email("销售报告")
# 添加内容
email.add_widget(
TextWidget().set_content("销售数据分析").set_type(TextType.TITLE_LARGE)
)
return email
# 4. 主函数
def main():
data = prepare_data()
email = build_email(data)
email.export_html("report.html")
🚀 Next Steps¶
Now that you understand EmailWidget's core concepts, you can:
- Check out User Guide to learn detailed component usage
- Browse API Reference to understand the complete API
- Study Example Code to learn practical applications
- Read Development Guide to participate in project development