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: |
|
---|
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: |
|
---|
Returns: |
|
---|
__iadd__(other)
codify()
Turn the HTML
object into Python code
Returns: |
|
---|
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: |
|
---|
Returns: |
|
---|
from_url(url)
classmethod
Parse the HTML from the given url and return a new instance of this class
Parameters: |
|
---|
Returns: |
|
---|
raw_text(text)
Add raw, unescaped text to the HTML
object.
Parameters: |
|
---|
Returns: |
|
---|
text(text, escape=True)
Add text to the current HTML object.
Parameters: |
|
---|
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>