Skip to main content

Library

nate

PyPI PyPI - License Build Status

nate makes generating HTML fun. Rather than forcing you to adopt an entirely different templating language that comes with its own set of quirks, nate is built around a simple, but powerful Python DSL that enables you to easily compose trees of elements that can be translated to well-formed HTML - no more forgotten angle brackets, unbalanced tags, or unescaped user input.

Think of nate as an alternative to Jinja or Django templates.

Features

  • Mostly typed - The API has been designed with type safety in mind: All function boundaries have type hints to ensure correctness, ease documentation, and make auto-completion in the IDE of your choice a piece of cake.
  • No dependencies - nate does not depend on any third-party libraries.
  • Stable API - The API itself is stable. No breaking changes are planned.
  • Tiny - The core library is approximately 500 eLOC, which is tiny and can be manually audited on a single afternoon.

Install

With pip installed, run

$ pip install nate

Usage

Import the elements you need.

from nate import Table, Thead, Th, Tr, Tbody, Td

Construct your tree.

politicians = [
    {"first_name": "Theodor", "last_name": "Heuss", "party": "FDP"},
    {"first_name": "Heinrich", "last_name": "Lübke", "party": "CDU"},
    {"first_name": "Gustav", "last_name": "Heinemann", "party": "SPD"},
    # ...
]

table = Table(
    [
        Thead(Th([Tr("First Name"), Tr("Last Name"), Tr("Party")])),
        Tbody(map(
            lambda politician: Tr(
                [
                    Td(politician["first_name"]),
                    Td(politician["last_name"]),
                    Td(politician["party"]),
                ]
            ),
            politicians,
        )),
    ]
)

Call .to_html() on your root node to serialize your tree to a string of HTML.

table.to_html()  #=> <table><thead>....

Raw text nodes are escaped by default, thus making it difficult to introduce XSS vulnerabilities.

p = P("<script>alert('XSS');</script>")
p.to_html()  #=> <p>&lt;script&gt;alert(&#x27;XSS&#x27;);&lt;/script&gt;</p>

Components

Templating languages tend to come with their own abstractions for building re-usable components. There is no need for those in nate, given that component hierarchies can easily be composed using plain Python functions.

from nate import Div, H1, P, BaseTag


def MyComponent(title: str, description: str) -> BaseTag:
    return Div(
        children=[
            H1(title),
            P(description),
        ],
        class_="my-component",
    )

component = MyComponent(
    title="My title",
    description="My description",
)

component.to_html()  #=> <div class="my-component>...

Examples

nate HTML
Title("Hello World!").to_html()
<title>Hello World!</title>
steaks = ["Rib Eye", "New York Strip", "Porterhouse"]
Ul(map(lambda steak: Li(steak), steaks)).to_html()
<ul>
  <li>Rib Eye</li>
  <li>New York Strip</li>
  <li>Porterhouse</li>
</ul>
Html(
    lang="en",
    children=[
        Head(
            children=[
                Meta(charset="utf-8"),
                Title(children="Welcome to nate!"),
            ]
        ),
        Body(
            children=[
                H1("Mission"),
                P(
                    "nate is not a template engine.",
                    class_="red",
                ),
            ],
        ),
    ],
).to_html()
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>Welcome to nate!</title>
    </head>
    <body>
        <h1>Mission</h1>
        <p class="red">nate is not a template engine.</p>
    </body>
</html>

How to contribute

  • Keep it simple, don't do anything too crazy. Even folks that don't know Python should be able to understand the code without any issues.
  • Design APIs with type-safety in mind.
  • If your code slows things down, it won't get merged.
  • Avoid introducing new dependencies.
  • If you don't want to follow those rules, forking is encouraged!
  • Ensure new code is covered by corresponding tests.

Prior works of art

  • lamernews - While written in Ruby, the page.rb library inspired this project.
  • hyperscript - Pure JavaScript alternative to JSX.
  • hyperpython - Python interpretation of hyperscript.
  • XHPy - Extends Python syntax such that XML document fragments become valid Python expressions. Based off XHP, a similar framework for PHP.

License

MIT

Metadata

Release files for nate 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nate 0.0.7
File Size Uploaded
nate-0.0.7.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nate 0.0.7
File Interpreter ABI Platform
nate-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / nate-0.0.7.tar.gz

Download URL nate-0.0.7.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
123c84298152e271e51ba766dd3b3d0f260c9b7ec6bd6d9371780ec333903970
BLAKE2b-256 checksum
How to use checksums
b023b10e6812a8ad5c9d64d8fac49b05f4c0120708b53a8a4b144e4a3bfce01d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / nate-0.0.7-py3-none-any.whl

Download URL nate-0.0.7-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71c9da8c4e554f58b59e20ce7b734d24fe164f8c3a738ff20f1d9a682a4a21d8
BLAKE2b-256 checksum
How to use checksums
975649112157f7272a8ae15fd6cb215f389821030dbedb0a106023e5349cad97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page