Skip to main content

Template-less html rendering in Python

Project description

simple_html

Template-less. Type-safe. Minified by default. Fast.

simple_html allows you to create HTML in standard Python. Benefits include:

  • typically faster than jinja2 -- up to 15x faster
  • typically renders fewer bytes than template-based rendering
  • types let your editor and tools help you write correct code faster
  • lightweight and framework agnostic
  • always renders valid html

Installation

pip install simple-html

Usage

from simple_html import div, h1, render, p

node = div({},
           h1({"id": "hello"},
              "Hello World!"),
           p({},
             "hooray!"))

render(node)  
# <div><h1 id="hello">Hello World!</h1><p>hooray!</p></div> 

There are several ways to render nodes:

from simple_html import br, div, h1, img, render

# raw node
render(br)
# <br/>

# node with attributes only
render(img({"src": "/some/image/url.jpg", "alt": "a great picture"}))
# <img src="/some/image/url.jpg" alt="a great picture"/>

# node with children
render(
    div({},
        h1({},
           "something"))
)
# <div><h1>something</h1></div>'

Tag attributes with None as the value will only render the attribute name:

from simple_html import div, render

render(
    div({"empty-str-attribute": "", 
         "key-only-attr": None})
)
# <div empty-str-attribute="" key-only-attr></div>

You can render inline css styles with render_styles:

from simple_html import div, render, render_styles

styles = render_styles({"min-width": "25px"})

render(
    div({"style": styles}, 
        "cool")
)
# <div style="min-width:25px;">cool</div>


# ints and floats are legal values
styles = render_styles({"padding": 0, "flex-grow": 0.6})

render(
    div({"style": styles},
        "wow")
)
# <div style="padding:0;flex-grow:0.6;">wow</div>

Lists and generators are both valid collections of nodes:

from typing import Generator
from simple_html import div, render, Node, br


def get_list_of_nodes() -> list[Node]:
    return ["neat", br]


render(div({}, get_list_of_nodes()))
# <div>neat<br/></div>


def node_generator() -> Generator[Node, None, None]:
    yield "neat"
    yield br


render(
    div({}, node_generator())
)
# <div>neat<br/></div>

For convenience, many tags are provided, but you can also create your own:

from simple_html import Tag, render

custom_elem = Tag("custom-elem")

# works the same as any other tag
node = custom_elem(
    {"id": "some-custom-elem-id"},
    "Wow"
)

render(node)  # <custom-elem id="some-custom-elem-id">Wow</custom-elem>

Strings are escaped by default, but you can pass in SafeStrings to avoid escaping.

from simple_html import br, p, SafeString, render

node = p({},
         "Escaped & stuff",
         br,
         SafeString("Not escaped & stuff"))

render(node)  # <p>Escaped &amp; stuff<br/>Not escaped & stuff</p> 

Attributes are also escaped -- both names and values. You can use SafeString to bypass, if needed.

from simple_html import div, render, SafeString

escaped_attrs_node = div({"<bad>":"</also bad>"})

render(escaped_attrs_node)  # <div &amp;lt;bad&amp;gt;="&amp;lt;/also bad&amp;gt;"></div>

unescaped_attrs_node = div({SafeString("<bad>"): SafeString("</also bad>")})

render(unescaped_attrs_node)  # <div <bad>="</also bad>"></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 Distribution

simple_html-1.2.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

simple_html-1.2.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple_html-1.2.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.4 Darwin/23.1.0

File hashes

Hashes for simple_html-1.2.2.tar.gz
Algorithm Hash digest
SHA256 8a428ec793d12562af1ea6c55f8e1c1b02dc25e0449b0245b6bdb4f3bc6ff560
MD5 48748dcc8fe95ac442c6c7ea367eeb9c
BLAKE2b-256 b13e5e3bb134a1b2d195898d5e3fbf6a8de7d6e372b346b165d53ba9e1d4d802

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_html-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.4 Darwin/23.1.0

File hashes

Hashes for simple_html-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 be94b204fbd205d7d4d874e9bbcba2ace588eb6d4dd84e90f454a2ccbd245558
MD5 5b3815b7cd859355b503a564d8fd2480
BLAKE2b-256 ba1445d5fb1a7a7afb1d8fcaed21bf7be49047ccc7fd24630bbb5c46900cb214

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