Skip to main content

htmlobj

Project description

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.

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

htmlobj-1.20.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

htmlobj-1.20.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file htmlobj-1.20.0.tar.gz.

File metadata

  • Download URL: htmlobj-1.20.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for htmlobj-1.20.0.tar.gz
Algorithm Hash digest
SHA256 62c488d845318a2dab5fc44a03be49f3acb1e59a6687d288c95db6c0d36827bc
MD5 681c43c32931eee35b1c6b77757392c2
BLAKE2b-256 af8f0fd19a215b72f6d94e50e0678c767bafb51316a162972216078aa16cfad8

See more details on using hashes here.

File details

Details for the file htmlobj-1.20.0-py3-none-any.whl.

File metadata

  • Download URL: htmlobj-1.20.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for htmlobj-1.20.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0fb783b568676929faa7d215df70510d083075ae7efedf0dacb9e991d7f1c50
MD5 8921e172c9de5e03d5a5f5ed607ec1c7
BLAKE2b-256 bf375883b8f983ce48bc1dfcae63ae75f61123b0a2aff58607dc21458b08379d

See more details on using hashes here.

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