Python-Generatable HTML
⚠️ In per-issue development
🦐 Any help or critique is encouraged.
This library was born when I needed to convert English lesson plans from JSON to a static website. I didn't want to learn how to set up a server just to compile some pages. And I don't know PHP. So this library consists of HTML tags as Python classes, with attributes as class properties.
💅 It's pie-tea-em-el, actually.
Boolean Python values behave according to the logic of boolean HTML attributes. autofocus
is turned off by default, so autofocus=False
doesn't lead to redundancies. The same goes for autocorrect="on"
, which is the default value.
import pyghtml as html
b = html.Button() + "Pyghtml"
b.title = "Home"
b.disabled = True
b.autofocus = False # default
b.autocorrect = "on" # default
print(b)
produces:
<button title="Home" disabled>Pyghtml</button>
- Each container tag is a
MutableSequence
storing children in theinner_html
.append()
is the same as+= item
;.extend()
is the same as+= [items]
- You can use
with
to automatically append elements, so you don't forget
import pyghtml as html
with html.Div() as div:
html.Img(src="image.jpg")
p = html.P(inner_html="Hello")
p.class_ = "greeting"
p += " World!"
print(div)
produces:
<div>
<img src="image.jpg" />
<p class="greeting">Hello World!</p>
</div>
with
is safe to use while multi-threading html generation.
The contexts are stored inthreading.local()
.
Div().copy()
returns a shallow copy of the elementDiv().tag()
returnsdiv
Div(id="main", hidden=True).attrs_to_str()
returnsid="main" hidden
-
contains()
checks if an item/element is in a containerdeep=False
is equivalent toitem in container
deep=True
checks recursively
-
find_by_tag()
returns a list of children with the specified tagdeep=True
searches recursively
-
find_by_attr()
returns a list of children that support the attribute- optional
value
also matches the attribute's value deep=True
searches recursively
- optional
-
sub()
substitutes children within a containercount
limits the number of replacementsdeep=True
replaces recursively
Run:
pip install pyghtml
Import:
import pyghtml as html
-
Self-closing tags are self-closing (
<br />
). -
<!DOCTYPE html>
isDoctype()
and<!-- -->
isCommentHtml()
. -
HTML attributes that match Python reserved words are suffixed with
_
(class
isclass_
). -
If the attribute's default value is
None
, that usually means there is no clear default value for it in the HTML spec (like the values controlled by the client-side agents when omitted). Attributes likealt
default toNone
because an omittedalt
is not the same asalt=""
from the accessibility standpoint. -
data-*
,aria-*
, event (onclick
, etc), and custom/missing attributes should be given as dictionaries throughdata_attrs
,aria_attrs
,event_attrs
,custom_attrs
. For example:data_attrs = {"data-custom-name": "custom value"}
. -
Abbreviations are broken:
Html()
,Wbr()
. This may be debatable, but I thought that classes likeHttpEquiv()
andAriaAttrs()
reflecthttp-equiv
andaria-*
much more clearly thanHTTPEquiv()
andARIAAttrs()
. Also, it's hard to keep in mind whetherbdi
,bdo
,src
,ul
,li
,dt
,dfn
count as abbreviations. -
There are class names like
I()
for<i></i>
, so please use a font that differentiates betweenIl1
. I use JetBrains Mono.
- Add attribute docstrings
- Add tag docstrings
- Add attribute validation
- Add tag validation