Skip to main content

simple, elegant HTML generation

Project description

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
<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 text')
>>> h.p
>>> print h
<p>hello, world!
more text</p>
<p>

Any HTML-specific characters (<>&") in the text will be quoted as appropriate. Note also that the top-level HTML object adds newlines between tags by default.

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('item 2 > 1')
>>> print h
<ol>
<li>item 1</li>
<li>item 2 &gt; 1</li>
</ol>

Note that the default behavior with lists (and tables) is to add newlines between sub-tags to generate a nicer output. The alternative to the above method is to use the containter tag as a context for adding the sub-tags:

>>> 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.

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>

This code is copyright 2009 eKit.com Inc (http://www.ekit.com/) See the end of the source file for the license of use.

Project details


Download files

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

Source Distribution

html-1.0.tar.gz (3.5 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for html-1.0.tar.gz
Algorithm Hash digest
SHA256 2344d9ef6d8663288e234b50894f313982e4c159f6b7700e4461c2ca8f5c3862
MD5 948478f84d61128723822f933af37bdf
BLAKE2b-256 cf91fb9cab8036ef982e3114284aa21c1530a5734531207a63828e9ae5900fd6

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