ButtonWidget API¶
ButtonWidget(widget_id=None)
¶
Bases: BaseWidget
Create a beautiful button-style link.
This widget is used to create button-style links in emails, mainly for guiding users to click and jump to specified pages. Supports custom button text, link address, colors and styles.
Attributes:
Name | Type | Description |
---|---|---|
text |
str
|
Text content displayed on the button. |
href |
str
|
Link address to jump to after clicking the button. |
background_color |
str
|
Background color of the button. |
text_color |
str
|
Text color of the button. |
width |
Optional[str]
|
Width of the button, can be pixel value or percentage. |
align |
str
|
Alignment of the button (left, center, right). |
Examples:
Create a basic button:
from email_widget.widgets import ButtonWidget
# Create a simple button
button = ButtonWidget()
button.set_text("Click to View Details")
button.set_href("https://example.com/details")
# Use method chaining to create custom styled button
button_custom = (ButtonWidget()
.set_text("Buy Now")
.set_href("https://shop.example.com")
.set_background_color("#22c55e")
.set_text_color("#ffffff")
.set_width("200px")
.set_align("center"))
# Create fully configured button
button_full = (ButtonWidget()
.set_full_button("Free Trial", "https://example.com/trial")
.set_padding("12px 24px")
.set_font_size("16px")
.set_border_radius("8px"))
Initialize ButtonWidget.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
widget_id
|
Optional[str]
|
Optional Widget ID. |
None
|
Functions¶
get_template_context()
¶
Get template context data required for rendering
set_align(align)
¶
Set the button alignment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
align
|
str
|
Alignment method, possible values: "left", "center", "right". |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
When alignment method is invalid. |
Examples:
set_background_color(color)
¶
Set the button's background color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color
|
str
|
Color value, supports hex format (e.g., #3b82f6) or color names. |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_border(border)
¶
Set the button border style.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
border
|
Optional[str]
|
Border style, e.g., "2px solid #3b82f6". If None, no border. |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_border_radius(radius)
¶
Set the button border radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
radius
|
str
|
Border radius value, e.g., "6px". |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_font_size(size)
¶
Set the button text font size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Font size, e.g., "16px". |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_font_weight(weight)
¶
Set the button text font weight.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight
|
str
|
Font weight, e.g., "normal", "600", "bold". |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_full_button(text, href, background_color=None)
¶
Set button basic information at once.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Button text. |
required |
href
|
str
|
Link address. |
required |
background_color
|
Optional[str]
|
Optional background color. |
None
|
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_href(href)
¶
Set the button's link address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
href
|
str
|
Target link address. |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
When link format is invalid. |
Examples:
set_padding(padding)
¶
Set the button padding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
padding
|
str
|
Padding value, e.g., "10px 20px". |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_text(text)
¶
Set the text displayed on the button.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Button text content. |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
When text is empty. |
Examples:
set_text_color(color)
¶
Set the button text color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color
|
str
|
Color value, supports hex format or color names. |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples:
set_width(width)
¶
Set the button width.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
width
|
Optional[str]
|
Width value, can be pixel value (e.g., "200px") or percentage (e.g., "50%"). If None, the button will adapt width based on content. |
required |
Returns:
Name | Type | Description |
---|---|---|
ButtonWidget |
ButtonWidget
|
Returns self to support method chaining. |
Examples: