Skip to content

TextWidget API

TextWidget(widget_id=None)

Bases: BaseWidget

TextWidget is a component for displaying various types of text content, supporting multiple predefined text types and rich style configuration.

This widget supports multiple text types (titles, body text, captions, section headings, etc.), and provides rich style setting options. Section headings automatically add numbering.

Main features: - Support for multiple predefined text types - Custom fonts, colors, alignment and other styles - Automatic section numbering - Multi-line text support - Responsive design

Attributes:

Name Type Description
_content str

Text content.

_text_type TextType

Text type.

_font_size str

Font size.

_align TextAlign

Alignment.

_color str

Text color.

_line_height str

Line height.

_font_weight str

Font weight.

_font_family str

Font family.

_margin str

Margin.

_max_width Optional[str]

Maximum width.

_section_number Optional[str]

Section number.

Examples:

Python
from email_widget.widgets import TextWidget
from email_widget.core.enums import TextType, TextAlign

# Basic usage
text = TextWidget().set_content("Hello World")

# Method chaining
title = (TextWidget()            .set_content("Important Title")            .set_type(TextType.TITLE_LARGE)            .set_color("#0078d4")            .set_align(TextAlign.CENTER))

# Section heading (automatic numbering)
section = TextWidget().set_content("Data Analysis").set_type(TextType.SECTION_H2)

Initialize TextWidget.

Parameters:

Name Type Description Default
widget_id Optional[str]

Optional widget ID.

None

Attributes

align property

Get current alignment.

Returns:

Name Type Description
TextAlign TextAlign

Current alignment enumeration value.

color property

Get current text color.

Returns:

Name Type Description
str str

Current text color CSS value.

content property

Get current text content.

Returns:

Name Type Description
str str

Current text content.

font_size property

Get current font size.

Returns:

Name Type Description
str str

Current font size CSS value.

Functions

get_template_context()

Get template context data required for rendering.

Returns:

Type Description
dict[str, Any]

Template context data dictionary

reset_section_numbers() staticmethod

Reset section number counters.

Reset all section number counters, typically called when starting a new document.

Examples:

Python Console Session
>>> TextWidget.reset_section_numbers()
>>> # Section titles created afterwards will start numbering from 1
set_align(align)

Set text alignment.

Parameters:

Name Type Description Default
align TextAlign

Alignment enum value.

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_align(TextAlign.CENTER)
>>> widget = TextWidget().set_align(TextAlign.RIGHT)
set_bold(bold=True)

Set whether text is bold.

Parameters:

Name Type Description Default
bold bool

Whether text is bold, defaults to True.

True

Returns:

Name Type Description
TextWidget TextWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_bold()  # Set to bold
>>> widget = TextWidget().set_bold(False)  # Cancel bold
set_color(color)

Set text color.

Parameters:

Name Type Description Default
color str

CSS color value (e.g., "#ff0000", "red", "rgb(255,0,0)").

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Raises:

Type Description
ValueError

When color format is invalid.

Examples:

Python Console Session
>>> widget = TextWidget().set_color("#ff0000")
>>> widget = TextWidget().set_color("blue")
set_content(content)

Set text content, supports multi-line text (separated by ).

Text Only
    Args:
        content (str): Text content.

    Returns:
        TextWidget: Supports method chaining.

    Raises:
        ValueError: When content is an empty string.

    Examples:
        >>> widget = TextWidget().set_content("Hello World")
        >>> # Multi-line text
        >>> widget = TextWidget().set_content("Line 1

Line 2 Line 3")

set_font_family(family)

Set font family.

Parameters:

Name Type Description Default
family str

CSS font family string.

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_font_family("Arial, sans-serif")
>>> widget = TextWidget().set_font_family("'Microsoft YaHei', SimHei")
set_font_size(size)

Set font size.

Parameters:

Name Type Description Default
size str

CSS font size value (e.g., "16px", "1.2em", "120%").

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Raises:

Type Description
ValueError

When size format is invalid.

Examples:

Python Console Session
>>> widget = TextWidget().set_font_size("18px")
>>> widget = TextWidget().set_font_size("1.5em")
set_font_weight(weight)

Set font weight.

Parameters:

Name Type Description Default
weight str

CSS font weight value (e.g., "normal", "bold", "600").

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_font_weight("bold")
>>> widget = TextWidget().set_font_weight("600")
set_italic(italic=True)

Set whether text is italic.

Parameters:

Name Type Description Default
italic bool

Whether text is italic, defaults to True

True

Returns:

Type Description
TextWidget

Returns self to support method chaining

Note

Current version has not implemented italic functionality, interface reserved

Examples:

Python Console Session
>>> widget = TextWidget().set_italic()  # Set to italic
set_line_height(height)

Set line height.

Parameters:

Name Type Description Default
height str

CSS line height value (e.g., "1.5", "24px", "150%").

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_line_height("1.8")
>>> widget = TextWidget().set_line_height("28px")
set_margin(margin)

Set margin.

Parameters:

Name Type Description Default
margin str

CSS margin value (e.g., "16px 0", "10px", "1em 2em").

required

Returns:

Name Type Description
TextWidget TextWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_margin("20px 0")
>>> widget = TextWidget().set_margin("10px")
set_max_width(max_width)

Set maximum width.

Parameters:

Name Type Description Default
max_width str

CSS maximum width value (e.g., "600px", "80%", "50em").

required

Returns:

Name Type Description
TextWidget TextWidget

Returns self to support method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_max_width("600px")
>>> widget = TextWidget().set_max_width("80%")
set_type(text_type)

Set text type, different types apply different preset styles.

Parameters:

Name Type Description Default
text_type TextType

Text type enum value.

required

Returns:

Name Type Description
TextWidget TextWidget

Supports method chaining.

Examples:

Python Console Session
>>> widget = TextWidget().set_type(TextType.TITLE_LARGE)
>>> widget = TextWidget().set_type(TextType.SECTION_H2)