Package: tinyhtml
A tiny library to safely render compact HTML5 from Python expressions.IntroductionThis is the entire API. The following documentation is longer than the
implementation.>>>fromtinyhtmlimporthtml,h,frag,rawThe most important function ish(). Below you see how to render attributes,
normal elements, and void/self-closing elements.>>>html(lang="en")(...h("head")(...h("meta",charset="utf-8"),...),...).render()'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"></head></html>'Usefrag()to pass around groups of elements.>>>frag(...h("h1")("Lorem ipsum ..."),...h("p")("... dolor sit amet."),...)raw('<h1>Lorem ipsum ...</h1><p>... dolor sit amet.</p>')Of course all content and attributes are properly escaped. Useraw()as an
escape hatch to render unescaped HTML.>>>print(h("a",title="&<>\"'")("&<>\"'").render())<atitle="&amp;&lt;&gt;&quot;'">&amp;&lt;&gt;"'</a>>>>print(raw("<!-- 💥"))<!--💥Installingpip install tinyhtmlFeatures and patternsSupports Python 3.7+.Output is compact: Naturally produces no superfluous whitespace between
elements.Fragments provide_repr_html_()for Jupyter Notebook integration.Includes mypy typings.>>>fromtinyhtmlimportFragWritetemplatesas functions.>>>deflayout(title:str,body:Frag)->Frag:...returnhtml()(...h("head")(...h("title")(title),...),...h("body")(body)...)>>>layout("Hello world",frag(...h("h1")("Hello world"),...h("p")("Lorem ipsum dolor sit amet."),...))raw('<!DOCTYPE html><html><head><title>Hello world</title></head><body><h1>Hello world</h1><p>Lorem ipsum dolor sit amet.</p></body></html>')Usestr,int, other fragments,None, or iterables of these aschild elements. (Note that rendering consumes the iterables, so fragments
using generators can be rendered only once.)>>>h("ul")(...h("li")(n)forninrange(3)...)raw('<ul><li>0</li><li>1</li><li>2</li></ul>')>>>h("ul")(...h("li")("Foo")ifFalseelseNone,...h("li")("Bar"),...)raw('<ul><li>Bar</li></ul>')Usestr,int,None, iterables of these,bool, or dictionaries
with boolean values asattributes.>>>h("input",type="checkbox",checked=True,disabled=False)raw('<input type="checkbox" checked>')>>>h("body",klass=["a","b"])()raw('<body class="a b"></body>')>>>h("body",klass={..."a":True,..."b":False,...})()raw('<body class="a"></body>')Useklassinstead ofclass, append a trailing underscore (for_),
or use underscores instead of dashes (http_equiv) for attribute names
that cannot be Python identifiers.>>>h("div",klass="container")()raw('<div class="container"></div>')>>>h("label",for_="name")("Name")raw('<label for="name">Name</label>')>>>h("meta",http_equiv="refresh",content=10)raw('<meta http-equiv="refresh" content="10">')Render fragments asstr, or into a list ofstrfor efficient string
building.>>>frag("Hello world","!").render()'Hello world!'>>>builder=[]>>>frag("Hello world","!").render_into(builder)>>>builder['Hello world','!']>>>"".join(builder)'Hello world!'Does not support comment nodes, unescapable raw text elements
(like inline styles and scripts), or foreign elements (like inline SVG).
Instead, reference external files, or useraw()with appropriate caution.AcknowledgementsInspired by (the good parts of)ScalaTags.LicenseLicensed under theApache License, Version 2.0,
or theMIT license, at your option.