Skip to main content

Write safer and cleaner HTML using Python

Project description

htmldoom

Write safer and cleaner HTML using Python

PyPI version PyPI version Build Status codecov Code style: black

Usage

A basic tag

>>> from htmldoom import elements as e
>>> 
>>> e.P(style=e.style(color="red"))("This is a paragraph")
<p style="color:'red';">This is a paragraph</p>

A builtin layout

>>> from htmldoom import elements as e
>>> from htmldoom.layouts import BaseLayout
>>> 
>>> class MyLayout(BaseLayout):
... 
...     @property
...     def title(self) -> e.Title:
...         return e.Title()(self["title"])
... 
...     @property
...     def body(self) -> e.Body:
...         return e.Body()(f"Welcome {self['user']['name']}")
... 
>>> MyLayout({"title": "foo", "user": {"name": "bar"}})
<!DOCTYPE html>
<html><head><title>foo</title></head><body>Welcome bar</body></html>

A functional style foreach loop with a switch case

>>> from htmldoom import elements as e
>>> from htmldoom import functions as fn
>>> 
>>> tuple(fn.foreach(["good", "bad", "evil"])(
...     lambda x: fn.switch({
...         x == "good": lambda: e.Span(style="color: green")(f"this is {x}"),
...         x == "bad": lambda: e.Span(style="color: yellow")(f"this is {x}"),
...         x == "evil": lambda: e.Span(style="color: red")(f"this is {x}"),
...         fn.Case.DEFAULT: lambda: fn.Error.throw(ValueError(x)),
...     })
... ))
(<span style="color: green">this is good</span>,
 <span style="color: yellow">this is bad</span>,
 <span style="color: red">this is evil</span>)

Find more examples here

Q/A

What is the goal here?

The primary goal is to make writing HTML cleaner, easier, safer and intuitive using Python.

What about performance?

Although performance is not the primary goal here, it should not be a roadblock. htmldoom is copying the syntax and properties of elm, an existing fast and purely functional programming language that specializes in rendering HTML in virtual doms. Elm does all the optimisation internally which I'm sure can be implemented in Python to a great extent.
All the elements and attributes in htmldoom are supposed to be immutable (unless you want to hack it for some reason). Being immutable enables them to be cachable and sharable making use of the flyweight pattern, thus improving the speed of rendering.
Furthermore, if we follow the the DOM size recommendations, i.e.

  • less than 1500 nodes total.
  • maximum depth of 32 nodes.
  • no parent node with more than 60 child nodes.
htmldoom performs really well (refer to the benchmarks).

Plugins and ecosystem

  • moodlmth: Convert raw HTML pages into python source code

Benchmarks

Very basic benchmark done using this script and IPython

htmldoom

htmldoom stats

Jinja2

Jinja2 stats

Mako

Mako stats

Chameleon

Chameleon stats

Conclusion

htmldoom performs best upto a certain number of loops which is generally high enough.
NOTE: These measurements are very naive and shows very basic information.

Contributing

Check out the contributing guidelines.

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

htmldoom-0.3.1.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

htmldoom-0.3.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file htmldoom-0.3.1.tar.gz.

File metadata

  • Download URL: htmldoom-0.3.1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for htmldoom-0.3.1.tar.gz
Algorithm Hash digest
SHA256 432e469c50df53d37ec2e1a254d1c9b3b5fe790bf8643682a84fc1f7e40a3a8f
MD5 253b3b264764d9f13a28f03443f0bac0
BLAKE2b-256 1aecbe031e1d6e58bc8c57f4b3fcac450c2df01c37b4a2c7712455db3e4a15c8

See more details on using hashes here.

File details

Details for the file htmldoom-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: htmldoom-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for htmldoom-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bcbbf018f6d260061e7c29b5708810135c91d076d587669a4ee8daa20512b718
MD5 bf1a77e6ce030517434b43674c7cc6dc
BLAKE2b-256 3469c005997ca8374b696c596d2a1f8c072982d3908dcc2d2d1a0879e6105927

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