API reference¶
Generated from docstrings and type-hints. The order matches the source so related classes stay together.
tagz (sync)¶
tagz - a typed HTML builder.
Sync flavour: html, Page, Tag, Fragment, Raw, Style, StyleSheet, ABSENT,
parse, TagParser, data_uri, open_data_uri.
Async streaming lives in tagz.aio - same symbol names, async signatures.
- class tagz.Fragment¶
Bases:
TagWrapper-less container - renders children inline with no surrounding tag.
- __init__(*_children, _escaped=True)¶
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
Yield ~``chunk_size`` character chunks for streaming into buffered I/O.
- iter_lines(indent_char='\\t')¶
Yield pretty-printed HTML line by line (no trailing
\n).
- iter_string(pretty=False)¶
Yield rendered HTML as small fragments. For streaming, prefer
iter_lines()oriter_chunk().
- class tagz.HTML¶
Bases:
HTMLBaseSync tag factory.
html.divandhtml["div"]return the sameTagInstancesubclass.
- class tagz.HTMLBase¶
Bases:
objectFactory machinery for sync/async
HTML. Subclasses bind__tag_fabric__.
- class tagz.Page¶
Bases:
PageBaseSync HTML5 document.
- to_html5(pretty=False)¶
Render document including
PREAMBLE.
- class tagz.PageBase¶
Bases:
object<html>/<head>/<body>composition shared by sync and asyncPage.Concrete subclasses must define
__html__(the tag factory) andto_html5(sync or async).- PREAMBLE: str = '<!doctype html>\n'¶
DOCTYPE emitted before
<html>. Override for a non-HTML5 doctype.
- __init__(body_element=None, head_elements=(), *args, **kwargs)¶
- class tagz.Raw¶
Bases:
FragmentWrapper-less verbatim content. No escaping - use only with trusted input (XSS risk otherwise).
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
Yield ~``chunk_size`` character chunks for streaming into buffered I/O.
- iter_lines(indent_char='\\t')¶
Yield pretty-printed HTML line by line (no trailing
\n).
- iter_string(pretty=False)¶
Yield rendered HTML as small fragments. For streaming, prefer
iter_lines()oriter_chunk().
- class tagz.Style¶
-
CSS declaration block. Underscores in keys map to hyphens; declarations are sorted by key.
- classmethod __new__(*args, **kwargs)¶
- clear() None. Remove all items from D.¶
- copy() a shallow copy of D¶
- classmethod fromkeys(iterable, value=None, /)¶
Create a new dictionary with keys from iterable and values set to value.
- get(key, default=None, /)¶
Return the value for key if key is in the dictionary, else default.
- items() a set-like object providing a view on D's items¶
- keys() a set-like object providing a view on D's keys¶
- pop(k[, d]) v, remove specified key and return the corresponding value.¶
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem()¶
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(key, default=None, /)¶
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- update([E, ]**F) None. Update D from dict/iterable E and F.¶
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- values() an object providing a view on D's values¶
- class tagz.StyleSheet¶
Bases:
dict[str|tuple[str, …],Style]Selector →
Stylemapping. Tuple keys render as a comma-separated selector list.- __init__(*args, **kwargs)¶
- classmethod __new__(*args, **kwargs)¶
- clear() None. Remove all items from D.¶
- copy() a shallow copy of D¶
- classmethod fromkeys(iterable, value=None, /)¶
Create a new dictionary with keys from iterable and values set to value.
- get(key, default=None, /)¶
Return the value for key if key is in the dictionary, else default.
- items() a set-like object providing a view on D's items¶
- keys() a set-like object providing a view on D's keys¶
- pop(k[, d]) v, remove specified key and return the corresponding value.¶
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem()¶
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(key, default=None, /)¶
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- update([E, ]**F) None. Update D from dict/iterable E and F.¶
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- values() an object providing a view on D's values¶
- class tagz.Tag¶
Bases:
TagBaseSync HTML element. Sync sibling of
tagz.aio.Tag.- to_string(pretty=False)¶
Render to a complete HTML string.
pretty=Trueindents and line-breaks.
- iter_string(pretty=False)¶
Yield rendered HTML as small fragments. For streaming, prefer
iter_lines()oriter_chunk().
- iter_lines(indent_char='\\t')¶
Yield pretty-printed HTML line by line (no trailing
\n).
- iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
Yield ~``chunk_size`` character chunks for streaming into buffered I/O.
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- __init__(_tag_name, *_children, classes=(), _void=False, _escaped=True, **attributes)¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- class tagz.TagBase¶
Bases:
objectData + internal helpers shared by sync and async tags.
The public render API lives on the concrete
tagz.Tag(sync) andtagz.aio.Tag(async) - keeping it off the base avoids a Liskov override conflict between sync and async signatures.- __init__(_tag_name, *_children, classes=(), _void=False, _escaped=True, **attributes)¶
- class tagz.TagInstance¶
Bases:
TagBase for factory-produced element classes (
html.div→TagDiv).- __init__(*_children, classes=(), **attributes)¶
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
Yield ~``chunk_size`` character chunks for streaming into buffered I/O.
- iter_lines(indent_char='\\t')¶
Yield pretty-printed HTML line by line (no trailing
\n).
- iter_string(pretty=False)¶
Yield rendered HTML as small fragments. For streaming, prefer
iter_lines()oriter_chunk().
- class tagz.TagParser¶
Bases:
HTMLParserBuilds
Tagobjects from HTML strings. Most users should callparse()instead.- __init__()¶
Initialize and reset this instance.
If convert_charrefs is True (the default), all character references are automatically converted to the corresponding Unicode characters.
- Return type:
None
- handle_starttag(tag, attrs)¶
- handle_startendtag(tag, attrs)¶
Handle self-closing/void tag.
- CDATA_CONTENT_ELEMENTS = ('script', 'style')¶
- check_for_whole_start_tag(i)¶
- clear_cdata_mode()¶
- close()¶
Handle any buffered data.
- feed(data)¶
Feed data to the parser.
Call this as often as you want, with as little or as much text as you want (may include ‘n’).
- get_starttag_text()¶
Return full source of start tag: ‘<…>’.
- getpos()¶
Return current line number and offset.
- goahead(end)¶
- handle_charref(name)¶
- handle_comment(data)¶
- handle_entityref(name)¶
- handle_pi(data)¶
- parse_bogus_comment(i, report=1)¶
- parse_comment(i, report=1)¶
- parse_declaration(i)¶
- parse_endtag(i)¶
- parse_html_declaration(i)¶
- parse_marked_section(i, report=1)¶
- parse_pi(i)¶
- parse_starttag(i)¶
- reset()¶
Reset this instance. Loses all unprocessed data.
- set_cdata_mode(elem)¶
- unknown_decl(data)¶
- updatepos(i, j)¶
- tagz.data_uri(data, media_type='application/octet-stream')¶
Encode bytes as a
data:URI.
- tagz.open_data_uri(file_path, media_type=None)¶
Read a file and encode as a
data:URI.media_typeis guessed from the extension if omitted.
tagz.aio (async)¶
Mirror of tagz with async render methods. See
Async and tagz.
Shared value types (Style, StyleSheet, ABSENT, AbsentAttribute)
and sync-only helpers (parse, TagParser, data_uri, open_data_uri)
are re-exported as-is — see the tagz section above for their docs.
Async mirror of tagz. Same symbols, async render - change the import line and add await.
Children and attributes may also be coroutines, awaitables, async-def functions, or async iterables. See Async and tagz for the contract.
- class tagz.aio.Fragment¶
Bases:
TagAsync wrapper-less container - children render flat with no surrounding tag.
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- async iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
- Return type:
- Parameters:
- class tagz.aio.Page¶
Bases:
PageBaseAsync HTML5 document.
- class tagz.aio.Raw¶
Bases:
FragmentAsync
tagz.Raw- unescaped verbatim content.- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- async iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
- Return type:
- Parameters:
- class tagz.aio.Tag¶
Bases:
TagBaseAsync HTML element. Sibling of
tagz.Tag; awaits any awaitable, iterates any__aiter__source.- async iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
- Return type:
- Parameters:
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- __init__(_tag_name, *_children, classes=(), _void=False, _escaped=True, **attributes)¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- class tagz.aio.TagInstance¶
Bases:
TagAsync factory base. Sibling of
tagz.TagInstance.- __init__(*_children, classes=(), **attributes)¶
- attributes: MutableMapping[str, str | None | Style | Callable[[], str | None | Style | AbsentAttribute]]¶
- append(other)¶
Append a child. Strings are escaped (unless
_escaped=False); callables run at render time.
- async iter_chunk(chunk_size=4096, pretty=False, indent_char='\\t')¶
- Return type:
- Parameters:
- tagz.aio.ensure_awaitable(maybe_awaitable)¶
Coerce a callable, coroutine, or future to an
Awaitable.Sync callables are scheduled via
loop.call_soonwith a capturedcontextvarscontext.
- tagz.aio.html = <tagz.aio.HTML object>¶
Async tag factory, sharing
tagz.base.HTML_DEFAULTSwith the sync factory.