Skip to main content

simple and pythonic way to write html/svg tags

Project description

tagit

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

simple and pythonic way to write html/svg tags

Key Features

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

Installation

pip install tagit

Quick Start

  • import common html elements
# all html/svg elements are available as functions
from tagit import div

div('hi', id='foo')
# <div id="foo" class="bar">hi</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
from tagit import label, input_

label('username', for_='username') + input_(type="text", id="username", class_="bar")
# <label for="username">username</label><input type="text" id="username" class="bar"/>
  • value-less(boolean) attributes. eg. checked, disabled, selected
  • to denote value-less attribute:
    • use empty string, eg. checked=""
    • use positional argument
div(img(src='url'), class_='bar', checked="")
# <div class="bar" checked><img src="url"/></div>

input_(None, 'disabled', type='text')
# <input disabled type="text"/>
  • create custom element
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>
  • more examples available at demo.py file and the 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?

Open a github issue or ping me on X x-icon

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.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

tagit-0.6.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tagit-0.6.1.tar.gz
  • Upload date:
  • Size: 6.8 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.1.tar.gz
Algorithm Hash digest
SHA256 72c385381c83aa74c9bf85e57068a2b51ff4e083919d9cffb3f75a12e6883564
MD5 b1b735f168481dcf8f177a98d0a72eaa
BLAKE2b-256 813d5abc5c25363c6b47495b8d0472aa9b92813d1b1c630db26c18a24cec9b94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tagit-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3794488bae23772f16f1b485e19ef6278dde158fc8410c04c989fbcf582bde4f
MD5 75c6c4134dafd8d90e9937c41985b620
BLAKE2b-256 a5d1be58b5bc41e3c1d007d07f44305fb64460cb0d6683afa8e6758b5a099828

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