Skip to main content

simple, elegant HTML/XHTML generation

Project description

Simple, elegant HTML generation.

Constructing your HTML

To construct HTML start with an instance of html.HTML(). Add tags by accessing the tag’s attribute on that object. For example:

>>> from html import HTML
>>> h = HTML()
>>> h.br
>>> print h                          # or print(h) in python 3+
<br>

If the tag should have text content you may pass it at tag creation time or later using the tag’s .text() method (note it is assumed that a fresh HTML instance is created for each of the following examples):

>>> p = h.p('hello world!\n')
>>> p.text('more &rarr; text', escape=False)
>>> h.p
>>> print h
<p>hello, world!
more &rarr; text</p>
<p>

Any HTML-specific characters (<>&") in the text will be escaped for HTML safety as appropriate unless escape=False is passed. Note also that the top-level HTML object adds newlines between tags by default. Finally in the above you’ll see an empty paragraph tag - tags with no contents get no closing tag.

If the tag should have sub-tags you have two options. You may either add the sub-tags directly on the tag:

>>> l = h.ol
>>> l.li('item 1')
>>> l.li.b('item 2 > 1')
>>> print h
<ol>
<li>item 1</li>
<li><b>item 2 &gt; 1</b></li>
</ol>

Note that the default behavior with lists (and tables) is to add newlines between sub-tags to generate a nicer output. You can also see in that example the chaining of tags in l.li.b. If you wished you could add attributes to those chained tags, eg: l.li(id="special").b.

The alternative to the above method is to use the containter tag as a context for adding the sub-tags. The top-level HTML object keeps track of which tag is the current context:

>>> with h.table(border='1'):
...   for i in range(2):
...     with h.tr:
...       h.td('column 1')
...       h.td('column 2')
...  print h
<table border="1">
<tr><td>column 1</td><td>column 2</td></tr>
<tr><td>column 1</td><td>column 2</td></tr>
</table>

Note the addition of an attribute to the <table> tag.

A variation on the above is to explicitly reference the context variable, but then there’s really no benefit to using a with statement. The following is functionally identical to the first list construction:

>>> with h.ol as l:
...   l.li('item 1')
...   l.li.b('item 2 > 1')

You may turn off/on adding newlines by passing newlines=False or True to the tag (or HTML instance) at creation time:

>>> l = h.ol(newlines=False)
>>> l.li('item 1')
>>> l.li('item 2')
>>> print h
<ol><li>item 1</li><li>item 2</li></ol>

That control is also available as the newlines attribute on the HTML or tag instance if you need to alter it after instantiation.

Since we can’t use class as a keyword, the library recognises klass as a substitute:

>>> print h.p(content, klass="styled")
<p class="styled">content</p>

How generation works

The HTML document is generated when the HTML instance is “stringified”. This could be done either by invoking str() on it, or just printing it. It may also be returned directly as the “iterable content” from a WSGI app function.

You may also render any tag or sub-tag at any time by stringifying it.

Tags with no contents (either text or sub-tags) will have no closing tag. There is no “special list” of tags that must always have closing tags, so if you need to force a closing tag you’ll need to provide some content, even if it’s just a single space character.

Rendering doesn’t affect the HTML document’s state, so you can add to or otherwise manipulate the HTML after you’ve stringified it.

Creating XHTML

To construct XHTML start with an instance of html.XHTML() and use it as you would an HTML instance. Empty elements will now be rendered with the appropriate XHTML minimized tag syntax. For example:

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

Version History (in Brief)

  • 1.8 added Python 3 compatibility

  • 1.7 added Python 2.5 compatibility and escape argument to tag construction

  • 1.6 added .raw_text() and and WSGI compatibility

  • 1.5 added XHTML support

  • 1.3 added more documentation, more tests

  • 1.2 added special-case klass / class attribute

  • 1.1 added escaping control

  • 1.0 was the initial release


This code is copyright 2009 eKit.com Inc (http://www.ekit.com/) See the end of the source file for the license of use. XHTML support was contributed by Michael Haubenwallner.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

html-1.8.zip (5.9 kB view details)

Uploaded Source

html-1.8.tar.gz (7.0 kB view details)

Uploaded Source

html-1.8.tar.bz2 (5.3 kB view details)

Uploaded Source

File details

Details for the file html-1.8.zip.

File metadata

  • Download URL: html-1.8.zip
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for html-1.8.zip
Algorithm Hash digest
SHA256 393251d590788a4a616988c8f623de0c72b0f2fddbeecf85b0c8f4fe3579a3ea
MD5 84da4f7c51892cc22885b7880991a6f8
BLAKE2b-256 0f440beb51a5f4c7727f9830b3fb5e35ffd7c1e329f44bc400cc87b236803c96

See more details on using hashes here.

File details

Details for the file html-1.8.tar.gz.

File metadata

  • Download URL: html-1.8.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for html-1.8.tar.gz
Algorithm Hash digest
SHA256 cb86b453ab148266a485434f01ea1a55ce1185d5044aa4646ed93f8a60e8053b
MD5 d55c77c4fb553419a6a2b02ff45b9995
BLAKE2b-256 0e1ceca1585a6d6d4aa8414245b9aa13217f5d2aabee82f27e3f24a6579620c5

See more details on using hashes here.

File details

Details for the file html-1.8.tar.bz2.

File metadata

  • Download URL: html-1.8.tar.bz2
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for html-1.8.tar.bz2
Algorithm Hash digest
SHA256 484063e652d3dd9b1b96967a178294eea034c14fcadc853357777352a2701f0d
MD5 b83d98b2734dcc0398b533448a169bf7
BLAKE2b-256 f8ae9369b32f8c9d76aa473f97aebe9911fd00555bb8ff2b4c0b0071b2871322

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page