Skip to main content

htmlobj

htmlobj allows you to easily create complex HTML (or XML, xHTML) using nothing but Python code. It is an alternative to using templates in web frameworks, which usually have their own language syntax.

Example:

from htmlobj import HTML

h = HTML("html")
h.head.title("My Page")  # can chain tags if only 1 subitem
with h.body:  # use `with` for multiple subitems
    h.p.u("Paragraph 1 underlined")
    with h.p("Paragraph 2 ", class_="p2"):  # add attributes too
        h.b("bold")  
        h.text(", not bold") # add additional text
print(h)

Which outputs:

<html>
<head>
<title>My Page</title>
</head>
<body>
<p><u>Paragraph 1 underlined</u></p>
<p class="p2">Paragraph 2 <b>bold</b>, not bold</p>
</body>
</html>

Note that the class_ attribute has a trailing underscore because class is a Python keyword.

New Features

htmlobj is a re-packaging of html3, with further Python 3 modernization and additional functionality added.

One added feature is creating an htmlobj.HTML instance from existing html, either as a string (HTML.from_html), or from a url (HTML.from_url):

h = HTML.from_url("https://example.com/")

This will often be used in combination with another new feature, HTML.codify, to generate Python code using htmlobj, for you. Start with a page that is similar to what you want to create, then modify as needed, e.g. to programatically fill in data for that page.

print(h.codify())

which gives output like:

h = HTML()
with h.html:
    with h.head:
        h.title("Example Domain")
        h.meta(charset="utf-8")
        h.meta(http-equiv="Content-type", content="text/html; charset=utf-8")
...

You can then copy this output as a starting point for you own code, to make a webpage similar to the one passed to from_url.

Note: you can also achieve a from_url / codify combination from the command line by running htmlobj.html_parser with python -m:

python -m htmlobj.html_parser https://example.com > my_code.py

Installation

pip install htmlobj

Next Steps

See Getting Started for more examples and detailed usage information.

Metadata

Release files for htmlobj 1.30.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for htmlobj 1.30.0
File Size Uploaded
htmlobj-1.30.0.tar.gz 22.1 kB Details

Built distribution (wheel)

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

Total release size: 33.4 kB

Release files / htmlobj-1.30.0.tar.gz

Download URL htmlobj-1.30.0.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ca4b268d83051b48db9272b0c2eff00137e3ddfb8515baf264ec522ddcdbbbd3
BLAKE2b-256 checksum
How to use checksums
2e811ad297c933814c05ec6a16ea2587041240562ca8524964132f6a0668f481
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 17, 2024.

Transparency log

Release files / htmlobj-1.30.0-py3-none-any.whl

Download URL htmlobj-1.30.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5d343757740e844837ea82e505d0a2266d2fb82945666179607cbc6b3f7e862
BLAKE2b-256 checksum
How to use checksums
e0b68a58c29722d153ba0bb8af159e86b03cb8feda2d78a60fc0d2aec49054a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 17, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

1.30.0 This release

2 release files

1.20.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