pyghtml
Python-Generatable HTML
⚠️ In per-issue development
🦐 Any help or critique is encouraged.
Idea
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.
Features
Attribute types and defaults
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>
Simple container logic
- Each container tag is a
MutableSequencestoring children in theinner_html .append()is the same as+= item;.extend()is the same as+= [items]- You can use
withto 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>
withis safe to use while multi-threading html generation.
The contexts are stored inthreading.local().
Utilities
Class methods
Div().copy()returns a shallow copy of the elementDiv().tag()returnsdivDiv(id="main", hidden=True).attrs_to_str()returnsid="main" hidden
Functions
-
contains()checks if an item/element is in a containerdeep=Falseis equivalent toitem in containerdeep=Truechecks recursively
-
find_by_tag()returns a list of children with the specified tagdeep=Truesearches recursively
-
find_by_attr()returns a list of children that support the attribute- optional
valuealso matches the attribute's value deep=Truesearches recursively
- optional
-
sub()substitutes children within a containercountlimits the number of replacementsdeep=Truereplaces recursively
Installation
Run:
pip install pyghtml
Import:
import pyghtml as html
Additional notes
-
Self-closing tags are self-closing (
<br />). -
<!DOCTYPE html>isDoctype()and<!-- -->isCommentHtml(). -
HTML attributes that match Python reserved words are suffixed with
_(classisclass_). -
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 likealtdefault toNonebecause an omittedaltis 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-equivandaria-*much more clearly thanHTTPEquiv()andARIAAttrs(). Also, it's hard to keep in mind whetherbdi,bdo,src,ul,li,dt,dfncount as abbreviations. -
There are class names like
I()for<i></i>, so please use a font that differentiates betweenIl1. I use JetBrains Mono.
Future improvements
- Add attribute docstrings
- Add tag docstrings
- Add attribute validation
- Add tag validation
Sources
Metadata
Release files for pyghtml 1.4.0
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 |
|---|---|---|---|---|
| pyghtml-1.4.0.tar.gz | 18.8 kB | Without provenance | Details for pyghtml-1.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyghtml-1.4.0-py3-none-any.whl | Python 3 | none | any | Details for pyghtml-1.4.0-py3-none-any.whl |
Total release size: 35.6 kB
Release files / pyghtml-1.4.0.tar.gz
| Download URL | pyghtml-1.4.0.tar.gz |
|---|---|
| Size | 18.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ad052584ebc72df65bc634370e53fd868733bcbd73bd2f7f634ab386b85cdb13
|
|
BLAKE2b-256 checksum How to use checksums |
7658d53e0d079407f678eccdba8b8fdf61f3475bba2ad2ffe5104425ff5ad0d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / pyghtml-1.4.0-py3-none-any.whl
| Download URL | pyghtml-1.4.0-py3-none-any.whl |
|---|---|
| Size | 16.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c841bbe4585421ba135d79c0459c2b3931b23d1915cb6c2bb0fff01ba1a679ce
|
|
BLAKE2b-256 checksum How to use checksums |
281bf4b4af9a20ea188fe0901cbab3a157ddf79888f582d8efe871a9ac564f9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for pyghtml 1.4.0
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.