Skip to main content

HTML 5 Generator

Project description

This library is currently in development. The API is subject to change. Feedback and suggestions are welcome!

Basic usage:

>>> from htmlgen import Division, Span
>>> div = Division()
>>> div.id = "my-block"
>>> div.append("This is ")
>>> span = Span()
>>> span.add_css_classes("important")
>>> span.append("important!")
>>> div.append(span)

A tree constructed like this can either be converted to a string:

>>> str(div)
'<div id="my-block">This is <span class="important">important!</span></div>'

Alternatively, all elements can be used as iterator, for example to return them from a WSGI callback:

>>> def application(env, start_response):
...     start_response("200 OK", [("Content-Type", "text/html")])
...     return div

There are two different ways to render children of HTML elements. The tree construction approach shown above is mainly suitable for elements with few children. The disadvantage of this approach is that the whole tree must be constructed in memory. An alternative way, best suited for custom sub-classes of elements, is to override the generate_children method of the Element class:

>>> class MyBlock(Division):
...     def __init__(self):
...         super(MyBlock, self).__init__()
...         self.id = "my-block"
...     def generate_children(self):
...         yield "This is "
...         span = Span()
...         span.add_css_classes("important")
...         span.append("important!")
...         yield span
>>> str(MyBlock())
'<div id="my-block">This is <span class="important">important!</span></div>'

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

htmlgen-0.5.tar.gz (19.4 kB view details)

Uploaded Source

File details

Details for the file htmlgen-0.5.tar.gz.

File metadata

  • Download URL: htmlgen-0.5.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for htmlgen-0.5.tar.gz
Algorithm Hash digest
SHA256 c33468e7c54cbc26d2a6c18942a28ab7d25e694a6955aca9dc0b41e27ca89728
MD5 9a9d9893a90828572308a8826ffd57ae
BLAKE2b-256 9ff132dc88701c7a023b46c9de9bee0b742e0b3ae197ae35472adbd76629ed47

See more details on using hashes here.

Supported by

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