Skip to main content

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?

Metadata

Release files for tagit 0.6.3

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

Source distribution (sdist)

Source distribution for tagit 0.6.3
File Size Uploaded
tagit-0.6.3.tar.gz 6.7 kB Details

Built distribution (wheel)

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

Total release size: 14.6 kB

Release files / tagit-0.6.3.tar.gz

Download URL tagit-0.6.3.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
52ed3c48c97f974327e694b95950895903957c0da3b2113e05d938d60c05eacd
BLAKE2b-256 checksum
How to use checksums
47c0443cfdbacd4d0dcb8e998853f0d5baa80f17bfbe4977b1c13d612c17871d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release files / tagit-0.6.3-py3-none-any.whl

Download URL tagit-0.6.3-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50e5732ca558f111a45e27a17c229dc3696efcdd7e60ed60c7ccc4ea90016930
BLAKE2b-256 checksum
How to use checksums
a30db9cedf65cb6249f651855709877d858f497f066a78f79ae7db2eb0951cca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.6.3 This release

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