Skip to main content

HTML/SVG tag generator for minimalist

Project description

tagit

ci-badge pypi-badge MIT-badge black-badge

HTML/SVG tag generator for minimalist

Key Features

  • all elements output are pure string, simple and easy to manipulate
  • no classes or objects, only functions
  • all standard html and svg elements are included
  • create nested child elements with list of strings and elements
  • able to create custom elements
  • create value-less(boolean) attributes with positional argument
    • handy for using with UnoCSS attributify mode
  • pure python, no external dependencies
  • high test coverage

Quick Start

  • installation: pip install tagit
  • basic signature
    • element(tag_content: str | list | None = None, *args, **kwargs) -> str
# common elements
from tagit import div, img, p, ul, li, label, input_,

# empty tag
print(div())
# <div />

# None content is ignored
print(div(None))
# <div />

# empty string content creates closing tag
print(div(""))
# <div></div>

# tag as content
print(div(img(src="url"), id="bar"))  
# <div id="bar"><img src="url"/></div>

# content mix with strings and tags
print(div(["foo", img(src="url"), "bar")])
# <div>foo<img src="url"/>bar</div>
  • use trailing underscore to work around python keyword and built-in functions
  • attributes:
    • class_ -> class
    • for_ -> for
  • elements:
    • del_ -> del
    • input_ -> input
    • map_ -> map
    • object_ -> object
print(label("foo", for_="bar"))
# <label for="bar">foo</label>

print(input_(None, class_="foo", name="bar", type="checkbox", value="baz"))
# <input name="bar" type="checkbox" value="baz"/>
  • position args -> value-less attribute.
    • boolean attribute: eg. checked, disabled, selected
    • assign tailwind classes with UnoCSS attributify mode
print(div("foo", "clear-both", "m-2", "rounded", id="baz"))
# <div clear-both m-2 rounded id="baz">foo</div>
  • keyword argument with value None is ignored
tag = div(None, "m-2", "rounded", id="baz", style=None) 
print(tag)  
# <div m-2 rounded id="baz" />
  • create custom element
  • signature:
    • tag(tag_name: str, tag_content: str | list | None = None, *args, **kwargs) -> str
from tagit import tag

tag('div')
# <div />'

tag('div', 'Hello', id='greeting', class_='text-bold')
# <div id="greeting" class="text-bold">Hello</div>

tag('input', type='text', required='')
# <input type="text" required="" />'

tag('ul', [tag('li', 'Item 1'), tag('li', 'Item 2')])
# <ul><li>Item 1</li><li>Item 2</li></ul>

tag('button', 'Click me', 'disabled', class_='btn')
# <button disabled class="btn">Click me</button>

tag('div', 'Content', 'data-custom', id='example', aria_hidden='true')
# <div data-custom id="example" aria-hidden="true">Content</div>

tag("MyElement", tag_content="foo", props="bar")
# <MyElement props="bar">foo</MyElement>
  • see demo.py for a full demo
  • more examples could be found in tests package

Motivation

When creating simple website, instead of separating python and template files like this:

<ul id="navigation">
  {% for item in navigation %}
  <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
  {% endfor %}
</ul>

I prefer a pure python approach like this:

ul(
    [
      li(
        a(item.caption, href=item.href)
      )
      for item in navigation
    ],
    id = "navigation"
)

It provides full intellisense, type checking, and all language features from the text editor, a much better DX.

Need Help?

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

tagit-0.6.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

tagit-0.6.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file tagit-0.6.2.tar.gz.

File metadata

  • Download URL: tagit-0.6.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1014-azure

File hashes

Hashes for tagit-0.6.2.tar.gz
Algorithm Hash digest
SHA256 d51d6590398bb4ea1a48c065835b1cd69239b84d0c5d11b636b70fdab2cb65f7
MD5 4a2bb2047e6a04f07265c0dd8d6462f1
BLAKE2b-256 1476db938bae4b3ed84936b1c6a50bd29852d4eb3268dd76431f3eb7a572153c

See more details on using hashes here.

File details

Details for the file tagit-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: tagit-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1014-azure

File hashes

Hashes for tagit-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7eaf3bc750b06efe37807a6435badfe668a3bfa417e6bfce472d0f63e60a4503
MD5 0be28fbedd636abe884ecf8d10589f78
BLAKE2b-256 a66eb8a70f294fa185cc4c6a7deff010aecd53ad364dd02bda082b03f52265c6

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