Skip to main content

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 SafeStrings 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 &amp; 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"))

div.attrs(("id", "some-id"), height(100))

div.attrs(id_("some-id"), height(100))

div.attrs(id_("some-id"), height="100")

# each would render to: <div id="some-id" height="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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_html-0.4.2.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_html-0.4.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file simple_html-0.4.2.tar.gz.

File metadata

  • Download URL: simple_html-0.4.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.5 Darwin/20.5.0

File hashes

Hashes for simple_html-0.4.2.tar.gz
Algorithm Hash digest
SHA256 1fb68812d58aaf1fc4642a73091da3e27f90e7bc539ebdc9b89d21f714ae6a41
MD5 b8440fc95bcd969ffa5b7b166e1eb6ec
BLAKE2b-256 da517abf62e8f095419ae9124666f768ee11c896399f9da881f9a800d70fc96a

See more details on using hashes here.

File details

Details for the file simple_html-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: simple_html-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.5 Darwin/20.5.0

File hashes

Hashes for simple_html-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b6a5de5ee89abc4543272b403113bb71b8faed0aae01294a351f953e1861bac9
MD5 c4db5f7791b788dcf5c23e95d61c8d16
BLAKE2b-256 0209e6f73320dcfaa71e1dfe9b4fb1a9f816365f457071c5d91f68489517c2d8

See more details on using hashes here.

Supported by

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