Skip to content

API

pyhtml.create_tag

create_tag(name: str, base: type[Tag] = Tag) -> type[Tag]

Create a new PyHTML tag definition.

PyHTML already provides definitions for all standard HTML tags, so you don't need to use this unless you are using a JavaScript library that defines custom HTML elements.

Parameters:

Name Type Description Default
name str

the name of the tag. This is used during rendering, as <{name}> ... </{name}>.

required
base type[Tag]

the base class to use for the custom tag. The new tag will inherit from this base class.

Tag

Returns:

Type Description
type[Tag]

type[Tag]: a new tag definition.

pyhtml.Tag

Base tag class

children instance-attribute

children = flatten_list(list(children))

Children of this tag

attributes instance-attribute

attributes = filter_attributes(attributes)

Attributes of this tag

_get_tag_name

_get_tag_name() -> str

Returns the name of the tag

_get_default_attributes

_get_default_attributes(given: dict[str, AttributeType]) -> dict[str, AttributeType]

Returns the default attributes for the tag given the specified attributes.

This is overridden by child classes to return a dictionary of default attributes that are applied to the class.

_get_tag_pre_content

_get_tag_pre_content() -> Optional[str]

Return "pre-content" for the tag.

This is used by the <html> tag to add a <!DOCTYPE html> before the tag.

_render

_render(indent: int) -> list[str]

Renders tag and its children to a list of strings where each string is a single line of output

_escape_children

_escape_children() -> bool

Returns whether the contents of the element should be escaped, or rendered plainly.

By default, all string content should be escaped to prevent security vulnerabilities such as XSS, but this is disabled for certain tags such as <script>.

pyhtml.SelfClosingTag

Bases: Tag

Self-closing tags don't contain child elements

pyhtml.WhitespaceSensitiveTag

Bases: Tag

Whitespace-sensitive tags are tags where whitespace needs to be respected.