Skip to main content

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?

Metadata

Release files for ptag 0.2.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 ptag 0.2.3
File Size Uploaded
ptag-0.2.3.tar.gz 7.6 kB Details

Built distribution (wheel)

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

Total release size: 16.3 kB

Release files / ptag-0.2.3.tar.gz

Download URL ptag-0.2.3.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
872039eb3b5c007315bca77491b410b56aa807bb70036eb134bff4efc7ad31c4
BLAKE2b-256 checksum
How to use checksums
13467ed2323bf564157bfd9fccf443b9721a42cca57bb890e8e80c41cb41023e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1014-azure

Release files / ptag-0.2.3-py3-none-any.whl

Download URL ptag-0.2.3-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10e7e237e71b31989a6abab84bdad968c2b9f2c0248448855f1d5fe136b3c5d0
BLAKE2b-256 checksum
How to use checksums
f1e53ae4393f561e1ed6e37c97ba11d886e514cb95f9711a43674bcc1e700027
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1014-azure

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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