Skip to main content

generate html/svg tags hierarchy with context manager

Project description

ptag

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

generate html/svg tags hierarchy with context manager

  • use ⭐️ context manager ⭐️ to create tag hierarchy
  • create value-less(boolean) attributes with positional argument
    • handy for using with UnoCSS attributify mode
  • all standard html and svg elements are exported as functions
  • pure python, no external dependencies
  • high test coverage

Quick Start

  • Installation: pip install ptag
  • base signature
    • element(content = None, *args, **kwargs) -> Tag
# common elements
from ptag import div, img, p, ul, li, label, input_,
# for creating custom element
from ptag import Tag  
# for pretty print
from ptag import prettify  

# 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 with context manager
with ul() as bullets:
    li("foo")
    li("bar")

print(bullets)
# <ul><li>foo</li><li>bar</li></ul>
  • pretty print
print(bullets.prettify())
# <ul>
#     <li>foo</li>
#     <li>bar</li>
# </ul>
  • 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" />
  • append content and attributes to existing tag
tag = div()
tag.affix(p("bar"), "m-2", "rounded", id="baz") 
print(tag)  
# <div m-2 rounded id="baz"><p>bar</p></div>
  • create custom element
  • signature:
    • Tag(name: str, content = None, *args, **kwargs) -> str
my_tag = Tag("MyTag", "foo", "bar", "corge", id="baz", class_="qux")
print(my_tag)  
# <MyTag bar corge id="baz" class="qux">foo</MyTag>
  • more examples could be found in tests package

Limitations

  • prettify() method doesn't support attribute without value
    • use kwargs instead of positional args if prettifying is needed
    • eg. selected -> selected=""

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

ptag-0.2.3.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

ptag-0.2.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file ptag-0.2.3.tar.gz.

File metadata

  • Download URL: ptag-0.2.3.tar.gz
  • Upload date:
  • Size: 7.6 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 ptag-0.2.3.tar.gz
Algorithm Hash digest
SHA256 872039eb3b5c007315bca77491b410b56aa807bb70036eb134bff4efc7ad31c4
MD5 6c5bc924f61ec049d07ba44821369952
BLAKE2b-256 13467ed2323bf564157bfd9fccf443b9721a42cca57bb890e8e80c41cb41023e

See more details on using hashes here.

File details

Details for the file ptag-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: ptag-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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 ptag-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 10e7e237e71b31989a6abab84bdad968c2b9f2c0248448855f1d5fe136b3c5d0
MD5 4ff96e831169c3d21bc72e34a7f98ad4
BLAKE2b-256 f1e53ae4393f561e1ed6e37c97ba11d886e514cb95f9711a43674bcc1e700027

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