Skip to main content

HTML 5 Generator

Project description

Python HTML 5 Generator

License PyPI - Python Version Release PyPI Travis CI

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()
>>> = "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__()
... = "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 Distribution

htmlgen-2.0.0-py3-none-any.whl (47.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page