Skip to main content

HTML 5 Generator

Project description

https://img.shields.io/pypi/l/htmlgen.svg https://img.shields.io/github/release/srittau/python-htmlgen/all.svg https://img.shields.io/pypi/v/htmlgen.svg https://travis-ci.org/srittau/python-htmlgen.svg?branch=master

Library to generate HTML from classes.

Basic usage:

>>> from htmlgen import Division, Span
>>> Division("This is ", Span("important!"), "!")

A more verbose example:

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

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

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

Alternatively, all elements can be used as iterators, 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("important")
...         span.add_css_classes("important")
...         yield span
...         yield "!"
>>> 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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

htmlgen-0.99.1-py3-none-any.whl (46.0 kB view details)

Uploaded Python 3

htmlgen-0.99.1-py2-none-any.whl (46.0 kB view details)

Uploaded Python 2

File details

Details for the file htmlgen-0.99.1-py3-none-any.whl.

File metadata

File hashes

Hashes for htmlgen-0.99.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed30c3a8c48c4065e6c548ab3bfa14dceac77c38211bb1d758bced3889dc556
MD5 1f26da33afb3e7f5298e43265bb8834b
BLAKE2b-256 4e1bf6565acef206260ca7c685a6b3ca2f6d8b9bcf8c8f78343f872fe576eb34

See more details on using hashes here.

File details

Details for the file htmlgen-0.99.1-py2-none-any.whl.

File metadata

File hashes

Hashes for htmlgen-0.99.1-py2-none-any.whl
Algorithm Hash digest
SHA256 1975d6be38ee241cfaf1d5b89abf7726c704b412edd020bb2bd809cf815b7b97
MD5 da32c4a39f14b6f189ccaae1720e79ca
BLAKE2b-256 2c7b13b6ec46b8e699b361c4c898589ef92026f992e57b2e178a57d36eba12a7

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