Skip to content

AlertWidget API

AlertWidget(widget_id=None)

Bases: BaseWidget

Create a GitHub-style alert box (Admonition).

This widget is used to highlight specific information in emails, such as notes, tips, warnings, or important reminders. It supports multiple preset alert types, each with unique colors and icons to attract reader attention.

Attributes:

Name Type Description
content str

Main text content displayed in the alert box.

alert_type AlertType

Type of alert, determines its appearance (color and icon).

title Optional[str]

Custom title for the alert box. If not set, will use default title based on alert_type.

Examples:

Basic usage, creating a warning type alert box:

Python
from email_widget.widgets import AlertWidget
from email_widget.core.enums import AlertType

alert = AlertWidget()
alert.set_content("System will undergo maintenance in 5 minutes, please save your work in time.")
alert.set_alert_type(AlertType.WARNING)
alert.set_title("System Maintenance Notice")

# You can also use method chaining to simplify code:
alert_chained = (AlertWidget()
                 .set_content("New features are online, go experience them!")
                 .set_alert_type(AlertType.TIP)
                 .set_title("Product Update")
                 .set_icon("🎉"))

Initialize AlertWidget.

Parameters:

Name Type Description Default
widget_id Optional[str]

Optional Widget ID.

None

Functions

clear_title()

Clear the alert box custom title.

After calling this method, the alert box will display the default title based on alert_type.

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> alert = AlertWidget().set_title("Custom Title").clear_title()
get_template_context()

Get template context data required for rendering

set_alert_type(alert_type)

Set the alert type.

Different alert types will apply different colors and icons.

Parameters:

Name Type Description Default
alert_type AlertType

Alert type enumeration value.

required

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> alert = AlertWidget().set_alert_type(AlertType.WARNING)
set_content(content)

Set the main text content displayed in the alert box.

Parameters:

Name Type Description Default
content str

Alert content.

required

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Raises:

Type Description
ValueError

When content is empty.

Examples:

Python Console Session
>>> alert = AlertWidget().set_content("This is an important notice.")
set_full_alert(content, alert_type, title=None)

Set complete alert information at once.

This method allows setting alert content, type, and optional title simultaneously for convenient quick configuration.

Parameters:

Name Type Description Default
content str

Alert content.

required
alert_type AlertType

Alert type.

required
title str

Optional custom title.

None

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> alert = AlertWidget().set_full_alert("Operation successful!", AlertType.TIP, "Complete")
set_icon(icon)

Set the alert box custom icon.

Parameters:

Name Type Description Default
icon str

Icon character (such as emoji or Unicode character).

required

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> alert = AlertWidget().set_icon("🚀")
set_title(title)

Set the alert box custom title.

If not set, will use the default title based on alert_type.

Parameters:

Name Type Description Default
title str

Custom title text.

required

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> alert = AlertWidget().set_title("Important Notice")
show_icon(show=True)

Set whether to display the alert box icon.

Parameters:

Name Type Description Default
show bool

Whether to show the icon, defaults to True.

True

Returns:

Name Type Description
AlertWidget AlertWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> alert = AlertWidget().show_icon(False)  # Hide icon