Reference

htmlobj.HTML(name=None, text=None, stack=None, newlines=True, escape=True)

Easily generate HTML.

h = HTML()
with h.table:
    with h.tr:
        h.td("cell 1")
        h.td("cell 2")
h.p.u("List")
with h.ul:
    for i in range(3):
        h.li(f"Item {i}")

print(h)

Output:

'<table><tr><td>cell 1</td>...'

Create a new HTML instance

Parameters:
  • name (str | None) –

    html tag name to start with. Defaults to None.

  • text (str | None) –

    Text for inside html tag. Defaults to None.

  • stack (list | None) –

    Internal use - stack of contents. Defaults to None.

  • newlines (bool) –

    Whether to put newlines in string output. Defaults to True.

  • escape (bool) –

    Whether to 'escape' special html characters. Defaults to True.

newline_default_on = set('table ol ul dl span html head body '.split()) instance-attribute class-attribute

html tags which use newlines by default

__call__(*content, **kw)

'Magic method' called when adding attrs in brackets e.g. h.tag(...)

Raises:
  • TypeError

    if called with read (problem with some WSGI providers)

Returns:
  • HTML( HTML ) –

    A reference to this HTML instance

__iadd__(other)

Operator for +=. Add content to the current HTML object

Parameters:
  • other (str | HTML) –

    text, or HTML instance to add

Returns:
  • HTML( HTML ) –

    A reference to this instance

codify()

Turn the HTML object into Python code

Returns:
  • str( str ) –

    Python code to generate this HTML instance

Note

codify is usually used when the HTML instance has been created using from_url or from_html

from_html(html) classmethod

Parse the given html string and return a corresponding instance of this class

Parameters:
  • html (str) –

    The html text to create the htmlobj.HTML instance from

Returns:
  • HTML( HTML ) –

    A new instance of the HTML class

from_url(url) classmethod

Parse the HTML from the given url and return a new instance of this class

Parameters:
  • url (str) –

    A web-site url as accepted by urllib

Returns:
  • HTML( HTML ) –

    a new instance of the HTML class

raw_text(text)

Add raw, unescaped text to the HTML object.

Parameters:
  • text (str) –

    The text to add inside the html tag

Returns:
  • HTML( HTML ) –

    A reference to this HTML instance

text(text, escape=True)

Add text to the current HTML object.

Parameters:
  • text (str) –

    The text to add inside the html tag

  • escape (bool) –

    Whether to 'escape' the html for special characters. Defaults to True.

htmlobj.XHTML

Bases: HTML

Easily generate XHTML. Tags in empty_elements are self-terminated.

Example:

>>> from htmlobj import XHTML
>>> h = XHTML()
>>> h.p
>>> h.br
>>> print(h)
<p></p>
<br />

empty_elements = set('base meta link hr br param img area input col colgroup basefont isindex frame'.split()) instance-attribute class-attribute

Elements which can be self-closing, e.g. <br />

htmlobj.XML

Bases: XHTML

Easily generate XML. All tags with no contents are reduced to self-terminating tags.

Example:

>>> from htmlobj import XML
>>> h = XML('xml')
>>> h.p
>>> h.br('hi there')
>>> print(h)
<xml>
<p />
<br>hi there</br>
</xml>