Tags
Note
Tags, or Air Tag, are explained in the concepts document about tags.
In the spirit of helping our users, every Air Tag has copious documentation—enough that sometimes it breaks the documentation build process. Therefore, Air Tag that directly correspond to their HTML equivalents can be found in smaller, easier-to-compile pages.
What remains on this page are core Air Tag that either have great utility (Raw and Children come to mind), or are base classes for other tags.
Easy to write and performant HTML content generation using Python classes to render HTML.
Tag
Tag(*children)
Bases: Transparent
Alias for the Transparent tag; use it if it improves clarity.
Source code in src/air/tags/models/special.py
42 43 44 45 46 | |
Raw
Raw(text_child='', /, **attributes)
Bases: UnSafeTag, Transparent
Renders raw HTML content without escaping.
Raises:
| Type | Description |
|---|---|
TypeError
|
If non-string content is provided |
Example: Raw('Bold text') # Produces 'Bold text' # Use with other tags Div( P("Safe content"), Raw('
'), P("More safe content") )
Source code in src/air/tags/models/special.py
86 87 88 89 90 91 92 93 94 | |
Children
Children(*children)
Bases: Transparent
Alias for the Transparent tag; use it if it improves clarity.
Source code in src/air/tags/models/special.py
42 43 44 45 46 | |
SafeStr
Bases: UserString
String subclass that bypasses HTML escaping when rendered.
BaseTag
BaseTag(*children, **attributes)
Base tag for all other tags.
Sets four attributes, name, module, children, and attrs. These are important for Starlette view responses, as nested objects get auto-serialized to JSON and need to be rebuilt. With the values of these attributes, the object reconstruction can occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
Renderable
|
Renderable objects that become the tag's inner content. |
()
|
attributes
|
AttributeType
|
Attribute names and values applied to the tag element. |
{}
|
Source code in src/air/tags/models/base.py
74 75 76 77 78 79 80 81 82 83 84 | |
__eq__
__eq__(other)
Compare tags by their rendered HTML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Object to compare against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the rendered HTML matches. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If compared to a non-BaseTag object. |
Source code in src/air/tags/models/base.py
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | |
__hash__
__hash__()
Return the hash of the rendered HTML representation.
Returns:
| Type | Description |
|---|---|
int
|
Hash derived from the rendered HTML string. |
Source code in src/air/tags/models/base.py
849 850 851 852 853 854 855 | |
__init_subclass__
__init_subclass__()
Register subclasses so they can be restored from serialized data.
Source code in src/air/tags/models/base.py
827 828 829 830 | |
__new__
__new__(*children, **attributes)
Create a tag instance while preventing direct BaseTag instantiation.
Raises:
| Type | Description |
|---|---|
TypeError
|
If code attempts to instantiate BaseTag directly. |
Source code in src/air/tags/models/base.py
86 87 88 89 90 91 92 93 94 95 | |
__repr__
__repr__()
Return a concise representation showing the tag name and summary.
Returns:
| Type | Description |
|---|---|
str
|
A readable string representation for debugging. |
Source code in src/air/tags/models/base.py
301 302 303 304 305 306 307 308 | |
__str__
__str__()
Render the HTML representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
The rendered HTML string. |
Source code in src/air/tags/models/base.py
293 294 295 296 297 298 299 | |
attrs
cached
property
attrs
Return the formatted HTML attributes string.
Returns:
| Type | Description |
|---|---|
str
|
A string containing formatted attributes prefixed with a space, |
str
|
or an empty string when no attributes are present. |
children
cached
property
children
Render all child nodes into a single HTML string.
Returns:
| Type | Description |
|---|---|
str
|
Concatenated child HTML, or an empty string when no children exist. |
compact_html
cached
property
compact_html
Render the compact-formatted HTML representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
A minimized HTML string produced by |
compact_render
compact_render()
Render the compact-formatted HTML representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
A minimized HTML string produced by |
Source code in src/air/tags/models/base.py
234 235 236 237 238 239 240 | |
first_attribute
property
first_attribute
Return the first attribute key-value pair or None when none exist.
Returns:
| Type | Description |
|---|---|
tuple[str, AttributeType] | None
|
The first attribute pair, or None if no attributes are set. |
first_child
property
first_child
Return the first child or None when no children are present.
Returns:
| Type | Description |
|---|---|
Renderable | None
|
The first child value, or None if there are no children. |
from_dict
classmethod
from_dict(source_dict)
Instantiate a tag hierarchy from serialized data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dict
|
TagDictType
|
The dictionary produced by |
required |
Returns:
| Type | Description |
|---|---|
BaseTag
|
The restored tag instance. |
Source code in src/air/tags/models/base.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
from_html
classmethod
from_html(html_source)
Reconstruct the corresponding air-tag tree from the given HTML content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html_source
|
str
|
HTML content to parse. |
required |
Returns:
| Type | Description |
|---|---|
BaseTag
|
The root air-tag built from the provided HTML content. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the markup is not valid HTML. |
Source code in src/air/tags/models/base.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 | |
from_html_file
classmethod
from_html_file(*, file_path)
Reconstruct the corresponding air-tag tree from the given HTML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
StrPath
|
The file path pointing to the HTML file or a folder with an index file to be read and parsed. |
required |
Returns:
| Type | Description |
|---|---|
BaseTag
|
The root air-tag built from the provided HTML file. |
Source code in src/air/tags/models/base.py
720 721 722 723 724 725 726 727 728 729 730 | |
from_html_file_to_source
classmethod
from_html_file_to_source(*, file_path)
Reconstruct the instantiable-formatted representation of the tag from the given HTML file.
For converting the corresponding air-tag tree from the given HTML file, into the instantiable-formatted representation of the tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
StrPath
|
The file path pointing to the HTML file or a folder with an index file to be read and parsed. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The formatted instantiation call for this tag and its children. |
Source code in src/air/tags/models/base.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
from_html_to_source
classmethod
from_html_to_source(html_source)
Reconstruct the instantiable-formatted representation of the tag from the given HTML content.
For converting the corresponding air-tag tree from the given HTML content, into the instantiable-formatted representation of the tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html_source
|
str
|
HTML content to parse. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The formatted instantiation call for this tag and its children. |
Source code in src/air/tags/models/base.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 | |
from_json
classmethod
from_json(source_json)
Instantiate a tag hierarchy from JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_json
|
str
|
The JSON string produced by |
required |
Returns:
| Type | Description |
|---|---|
BaseTag
|
The restored tag instance. |
Source code in src/air/tags/models/base.py
651 652 653 654 655 656 657 658 659 660 661 | |
full_repr
full_repr()
Return an expanded representation including attributes and children.
Returns:
| Type | Description |
|---|---|
str
|
The expanded string representation of the tag hierarchy. |
Source code in src/air/tags/models/base.py
319 320 321 322 323 324 325 326 327 328 329 330 | |
has_attributes
property
has_attributes
Return True when the tag defines one or more attributes.
Returns:
| Type | Description |
|---|---|
bool
|
True when attributes are present; otherwise False. |
has_children
property
has_children
Return True when the tag contains one or more children.
Returns:
| Type | Description |
|---|---|
bool
|
True when children are present; otherwise False. |
html
cached
property
html
Render the HTML representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
The rendered HTML string. |
is_attribute_free_void_element
property
is_attribute_free_void_element
Check whether the tag has neither attributes nor children.
Returns:
| Type | Description |
|---|---|
bool
|
True when the tag has no attributes and no children. |
last_attribute
property
last_attribute
Return the last attribute key-value pair or None when none exist.
Returns:
| Type | Description |
|---|---|
tuple[str, AttributeType] | None
|
The last attribute pair, or None if no attributes are set. |
last_child
property
last_child
Return the last child or None when no children are present.
Returns:
| Type | Description |
|---|---|
Renderable | None
|
The last child value, or None if there are no children. |
name
property
name
Return the normalized tag name.
Returns:
| Type | Description |
|---|---|
str
|
The lowercase tag name for use in HTML. |
num_of_attributes
property
num_of_attributes
Return the number of defined attributes.
Returns:
| Type | Description |
|---|---|
int
|
The count of attributes. |
num_of_direct_children
property
num_of_direct_children
Return the number of the direct children for an element.
Returns:
| Type | Description |
|---|---|
int
|
The count of children. |
pretty_display_in_the_browser
pretty_display_in_the_browser()
Display pretty-formatted HTML in the browser.
Source code in src/air/tags/models/base.py
262 263 264 | |
pretty_html
cached
property
pretty_html
Render prettified-formatted HTML representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
The prettified-formatted HTML string, |
pretty_print
pretty_print()
Display pretty-formatted HTML in the console with syntax highlighting.
Source code in src/air/tags/models/base.py
242 243 244 | |
pretty_render
pretty_render(
*, with_body=False, with_head=False, with_doctype=False
)
Render the prettified-formatted HTML representation of the tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_body
|
bool
|
Whether to wrap the HTML inside a |
False
|
with_head
|
bool
|
Whether to generate a |
False
|
with_doctype
|
bool
|
Whether to prefix the output with a doctype declaration. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The pretty-formatted HTML string. |
Source code in src/air/tags/models/base.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
pretty_render_in_the_browser
pretty_render_in_the_browser()
Render pretty-formatted HTML and open the result in a browser tab.
Source code in src/air/tags/models/base.py
211 212 213 | |
pretty_save
pretty_save(*, file_path)
Persist pretty-formatted HTML to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
StrPath
|
Destination path for the pretty HTML file. |
required |
Source code in src/air/tags/models/base.py
254 255 256 257 258 259 260 | |
print_source
classmethod
print_source(html_source)
Display the instantiable-formatted representation of the tag in the console with syntax highlighting.
- Reconstruct the corresponding air-tag tree from the given HTML content.
- Convert air-tag tree into the instantiable-formatted representation of the tag.
- Display it with syntax highlighting inside a styled terminal panel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html_source
|
str
|
HTML content to parse. |
required |
Source code in src/air/tags/models/base.py
663 664 665 666 667 668 669 670 671 672 673 674 | |
render
render()
Render the HTML representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
The rendered HTML string. |
Source code in src/air/tags/models/base.py
199 200 201 202 203 204 205 | |
render_in_the_browser
render_in_the_browser()
Render the tag and open the result in a browser tab.
Source code in src/air/tags/models/base.py
207 208 209 | |
save
save(*, file_path)
Persist the rendered HTML to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
StrPath
|
Destination path for the HTML file. |
required |
Source code in src/air/tags/models/base.py
246 247 248 249 250 251 252 | |
save_source
classmethod
save_source(*, file_path, html_source)
Save the instantiable-formatted representation of the tag to disk.
- Reconstruct the corresponding air-tag tree from the given HTML content.
- Convert air-tag tree into the instantiable-formatted representation of the tag.
- Save the Python expression that reconstructs this tag to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html_source
|
str
|
HTML content to parse. |
required |
file_path
|
StrPath
|
Destination path for the .py file. |
required |
Source code in src/air/tags/models/base.py
676 677 678 679 680 681 682 683 684 685 686 687 688 | |
tag_id
property
tag_id
Return the tag's id_ attribute when present.
Returns:
| Type | Description |
|---|---|
AttributeType | None
|
The |
to_dict
to_dict()
Convert the tag into a JSON-serializable dictionary.
Returns:
| Type | Description |
|---|---|
TagDictType
|
A mapping with the tag name, attributes, and serialized children. |
Source code in src/air/tags/models/base.py
580 581 582 583 584 585 586 587 588 589 590 | |
to_json
to_json(*, indent_size=None)
Serialize the tag to JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent_size
|
int | None
|
Indentation width to use for pretty-printing. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The JSON string representation of the tag. |
Source code in src/air/tags/models/base.py
600 601 602 603 604 605 606 607 608 609 | |
to_pretty_dict
to_pretty_dict(
*,
max_width=170,
max_length=7,
max_depth=4,
max_string=25,
expand_all=False,
)
Produce a human-friendly mapping view of the tag.
Returns:
| Type | Description |
|---|---|
str
|
A formatted string produced by the rich pretty printer when available, |
str
|
otherwise the standard string form of the mapping. |
Source code in src/air/tags/models/base.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
to_pretty_json
to_pretty_json()
Serialize the tag to formatted JSON.
Returns:
| Type | Description |
|---|---|
str
|
The indented JSON string representation of the tag. |
Source code in src/air/tags/models/base.py
611 612 613 614 615 616 617 | |
to_source
to_source()
Return a Python expression that reconstructs this tag.
Convert this air-tag into the instantiable-formatted representation of the tag.
Returns:
| Type | Description |
|---|---|
str
|
The formatted instantiation call for this tag and its children. |
Source code in src/air/tags/models/base.py
332 333 334 335 336 337 338 339 340 | |