Template-less html rendering in Python
Project description
simple_html
Template-less. Type-safe. Minified by default.
simple_html is built to simplify HTML rendering in Python. No templates needed. Just create HTML in normal Python. In most cases, the code will be more concise than standard HTML. Other benefits include:
- typically renders fewer bytes than template-based rendering
- types mean your editor and tools can help you write correct code faster
- no framework needed
- lightweight
Installation
pip install simple-html
Usage
from simple_html.nodes import body, head, html, p
from simple_html.render import render
node = html(
head,
body(
p.attrs(id="hello")(
"Hello World!"
)
)
)
render(node) # returns: <html><head></head><body><p id="hello">Hello World!</p></body></html>
Strings are escaped by default, but you can pass in SafeString
s to avoid escaping.
from simple_html.nodes import br, p, SafeString
from simple_html.render import render
node = p(
"Escaped & stuff",
br,
SafeString("Not escaped & stuff")
)
render(node) # returns: <p>Escaped & stuff<br/>Not escaped & stuff</p>
For convenience, many tags are provided, but you can create your own as well:
from simple_html.nodes import TagBase
from simple_html.render import render
custom_elem = TagBase("custom-elem")
render(
custom_elem.attrs(id="some-custom-elem-id")(
"Wow"
)
) # returns: <custom-elem id="some-custom-elem-id">Wow</custom-elem>
Likewise, some attributes have been created as type-safe presets. Note that there are multiple ways to create attributes. The examples below are all equivalent:
from simple_html.attributes import height, id_
from simple_html.nodes import div
# **kwargs: recommended for most cases
div.attrs(id="some-id", height="100")
# *args: useful for attributes that may be reserved keywords or when type constraints are desired.
# Presets, raw tuples, and kwargs can be used interchangeably.
div.attrs(id_("some-id"),
height(100),
("class", "100"),
width="100")
# renders to: <div id="some-id" height="100" class="100" width="100"></div>
You can build your own presets, using str_attr
, int_attr
, or bool_attr
. For instance, here are
several of the attribute preset definitions
from simple_html.attributes import bool_attr, int_attr, str_attr
checked = bool_attr('checked')
class_ = str_attr('class')
cols = int_attr('cols')
But anything that renders to the type of Attribute
will work.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for simple_html-0.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d51ebcfc7561f736b882a3396927d61743daa8b8a73bcd6313a65b2eb10a6929 |
|
MD5 | b9024469bf74770fd48ae2cf7d7ee0e7 |
|
BLAKE2b-256 | 697234aea87342438e3f1eef255300ea6c989fd40266f73bc819d2eb43624d40 |