Skip to main content

pythonic way to create HTML/XML/SVG tags

Project description

ptag

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

pythonic way to create HTML/XML/SVG tags

  • create tags in pure python
  • use context manager for 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

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>

# position args -> value-less attribute
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
print(div("foo", "bar", "m-2", "rounded", id="baz"))
# <div bar 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
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 on tests

Limitations

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

Need Help?

git-logo github issue

x-logo posts

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.2.tar.gz (7.6 kB view hashes)

Uploaded Source

Built Distribution

ptag-0.2.2-py3-none-any.whl (8.6 kB view hashes)

Uploaded Python 3

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