Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/srittau/python-htmlgen
Python HTML 5 Generator
https://github.com/srittau/python-htmlgen
html html5 python python2 python3
Last synced: about 1 month ago
JSON representation
Python HTML 5 Generator
- Host: GitHub
- URL: https://github.com/srittau/python-htmlgen
- Owner: srittau
- License: mit
- Created: 2014-03-06T09:44:51.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2023-06-26T12:09:20.000Z (over 1 year ago)
- Last Synced: 2023-11-20T15:44:33.796Z (about 1 year ago)
- Topics: html, html5, python, python2, python3
- Language: Python
- Size: 269 KB
- Stars: 16
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-html - srittau/python-htmlgen
- awesome-python-html - srittau/python-htmlgen
README
# Python HTML 5 Generator
[![License](https://img.shields.io/pypi/l/htmlgen.svg)](https://pypi.python.org/pypi/htmlgen/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/htmlgen)](https://pypi.python.org/pypi/htmlgen/)
[![Release](https://img.shields.io/github/release/srittau/python-htmlgen/all.svg)](https://github.com/srittau/python-htmlgen/releases/)
[![PyPI](https://img.shields.io/pypi/v/htmlgen.svg)](https://pypi.python.org/pypi/htmlgen/)
[![Travis CI](https://travis-ci.org/srittau/python-htmlgen.svg?branch=master)](https://travis-ci.org/srittau/python-htmlgen)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)
'This is important!'
>>> "This is {}!
".format(span)
'This is important!
'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 divThere 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())
'This is important!'