Skip to content

textual.content

Content is a container for text, with spans marked up with color / style. It is equivalent to Rich's Text object, with support for more of Textual features.

Unlike Rich Text, Content is immutable so you can't modify it in place, and most methods will return a new Content instance. This is more like the builtin str, and allows Textual to make some significant optimizations.

ANSI_DEFAULT module-attribute

ANSI_DEFAULT = Style(
    background=Color(0, 0, 0, 0, ansi=-1),
    foreground=Color(0, 0, 0, 0, ansi=-1),
)

A Style for ansi default background and foreground.

ContentText module-attribute

ContentText = Union['Content', Text, str]

A type that may be used to construct Text.

ContentType module-attribute

ContentType = Union['Content', str]

Type alias used where content and a str are interchangeable in a function.

TRANSPARENT_STYLE module-attribute

TRANSPARENT_STYLE = Style()

A null style.

Content

Content(text='', spans=None, cell_length=None)

Bases: Visual

Text content with marked up spans.

This object can be considered immutable, although it might update its internal state in a way that is consistent with immutability.

Parameters:

Name Type Description Default

text

str

text content.

''

spans

list[Span] | None

Optional list of spans.

None

cell_length

int | None

Cell length of text if known, otherwise None.

None

cell_length property

cell_length

The cell length of the content.

first_line property

first_line

The first line of the content.

markup cached property

markup

Get content markup to render this Text.

Returns:

Name Type Description
str str

A string potentially creating markup tags.

plain property

plain

Get the text as a single string.

spans property

spans

A sequence of spans used to markup regions of the content.

Warning

Never attempt to mutate the spans, as this would certainly break the output--possibly in quite subtle ways!

without_spans property

without_spans

The content with no spans

add_spans

add_spans(spans)

Adds spans to this Content instance.

Parameters:

Name Type Description Default

spans

Sequence[Span]

A sequence of spans.

required

Returns:

Type Description
Content

A Content instance.

append

append(content)

Append text or content to this content.

Note this is a little inefficient, if you have many strings to append, consider join.

Parameters:

Name Type Description Default

content

Content | str

A content instance, or a string.

required

Returns:

Type Description
Content

New content.

append_text

append_text(text, style='')

Append text give as a string, with an optional style.

Parameters:

Name Type Description Default

text

str

Text to append.

required

style

Style | str

Optional style for new text.

''

Returns:

Type Description
Content

New content.

assemble classmethod

assemble(*parts, end='')

Construct new content from string, content, or tuples of (TEXT, STYLE).

This is an efficient way of constructing Content composed of smaller pieces of text and / or other Content objects.

Example
content = Content.assemble(
    Content.from_markup("[b]assemble[/b]: "),  # Other content
    "pieces of text or content into a",  # Simple string of text
    ("a single Content instance", "underline"),  # A tuple of text and a style
)

Parameters:

Name Type Description Default

*parts

str | Content | tuple[str, str | Style]

Parts to join to gether. A part may be a simple string, another Content

()

end