ptag
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_->classfor_->for
- elements:
del_->delinput_->inputmap_->mapobject_->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
- boolean attribute: eg.
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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ptag-0.2.3.tar.gz | 7.6 kB | Without provenance | Details for ptag-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ptag-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for ptag-0.2.3-py3-none-any.whl |
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
|
Security for ptag 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.