Skip to main content

A python engine for building html components

Project description

Ketza - HTML defined in python

Contents

Rationale

This project provides a framework for defining reusable units of HTML in python code, as opposed to being a runtime or a templating engine.

My intention is to provide a powerful but simple interface, allowing for extensibility and interplay with other technologies without headaches.

Basics

At the core of Ketza is the tag function, which allows you to build a html element in a series of function calls as follows:

from ketza import tag

main = tag("main") # Apply element name

app = main({"id": "app"}) # Apply attributes as key-value pairs

hello_world = app("Hello World!") # Apply inner string content

# hello_world is now equal to `<main id="app">Hello World!</main>`

However, Ketza also provides pre-defined named tags for recognised HTML elements based on the WHATWG living standard. A further example is presented below, making use of predefined tags.

Example with an unordered list

Let's say, for argument's sake, I would like to define a list like this:

<ul id="list-of-three">
    <li class="list-triplet">
        Foo
    </li>
    <li class="list-triplet">
        Bar
    </li>
    <li class="list-triplet">
        Baz
    </li>
</ul>

This can be done as follows:

from ketza import ul, li

list_of_three = ul({"id": "list-of-three"})(
    li({"class": "list-triplet"})(
        "Foo"
    ),
    li({"class": "list-triplet"})(
        "Bar"
    ),
    li({"class": "list-triplet"})(
        "Baz"
    ),
)

However, we can simplify this even further:

from ketza import ul, li

triplet = li({"class": "list-triplet"})

list_of_three = ul({"id": "list-of-three"})(
    triplet("Foo"),
    triplet("Bar"),
    triplet("Baz")
)

For our purposes, this yields html which is functionally equivalent to our intended list-of-three. Even so, the raw html will look more like this:

<ul id="list-of-three"><li class="list-triplet">Foo</li><li class=.... 

While this does essentially result in pre-minified HTML output, there are situations which call for human-readable raw HTML. As such, the following section shall briefly touch on formatting.

Formatting

Ketza provides a utility function for indenting minified html:

from ketza.formatters import indent 

list_of_three = indent(list_of_three)

The indent function turns this:

<ul id="list-of-three"><li class="list-triplet">Foo</li><li class=.... 

Into this!

<ul id="list-of-three">
    <li class="list-triplet">
        Foo
    </li>
    <li class="list-triplet">
        Bar
    </li>
    <li class="list-triplet">
        Baz
    </li>
</ul>

Future Intentions

Though I am confident that Ketza provides the means to define components representing common HTML boilerplate, I am also considering defining utilities to further streamline HTML boilerplate creation.

I am also open to feedback for how I can improve and extend Ketza, as well as its interoperability with other web technologies.

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

ketza-2.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

ketza-2.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file ketza-2.0.1.tar.gz.

File metadata

  • Download URL: ketza-2.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for ketza-2.0.1.tar.gz
Algorithm Hash digest
SHA256 b1478b79b62e68e68e2faeb6a1e7b642901e447a141882b354ba139cc2b06006
MD5 b220b17bc305aca979d2c04d258df01b
BLAKE2b-256 3aceadb1a05d13c94ce71751b8a523467910bb93739b37c28ab900750af4831b

See more details on using hashes here.

File details

Details for the file ketza-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: ketza-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for ketza-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ad627d353c07a07d9d4805c9cd682b3b3775d9d1e653aae2d5b24065b1e4290
MD5 7f1e281b6b1f9c2b4581194cc0779e58
BLAKE2b-256 eea4999ad0b815828547cc946efe2d733c93fd8da3f40b0f9bb05c452d7ea864

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