Skip to main content

An intuitive, high performance HTML rendering framework

Project description

htmldoom

An intuitive, high performance HTML rendering framework

PyPI version PyPI version Build Status codecov Code style: black

Usage

A basic tag

>>> from htmldoom import render, elements as e
>>> 
>>> print(render(
...     e.textarea("required", class_="input")("text")
... ))
<textarea required class="input">text</textarea>

A fast dynamic elements rendering mechanism

Choose whichever syntax suits you:

Syntax 1

>>> from htmldoom import renders, elements as e
>>> 
>>> @renders(
...     e.p()("{x}"),
...     e.p()("another {x}"),
... )
... def render_paras(data: dict) -> dict:
...     return {"x": data["x"]}
>>> 
>>> render_paras({"x": "awesome paragraph"})
<p>awesome paragraph</p><p>another awesome paragraph</p>

Syntax 2

>>> from htmldoom import renders, elements as e
>>> 
>>> render_paras = renders(
...     e.p()("{x}"),
...     e.p()("another {x}"),
... )(lambda data: {"x": data["x"]})
>>> 
>>> render_paras({"x": "awesome paragraph"})
<p>awesome paragraph</p><p>another awesome paragraph</p>

NOTE: This mechanism compiles the template when the file loads and reuse it.

renders( -- compile-time code -- )( -- runtime code -- )

The more execution you move from runtime to compile-time, the faster it gets.
If you properly use this mechanism and refractor your dynamic pages into smaller components, it will surpass the performance of traditional template rendering engines.

WARNING: It performs a "{rendered_elements}".format(**returned_data). So be careful about where you put which code.

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)),
...     })
... ))
(b'<span style="color: green">this is good</span>',
 b'<span style="color: yellow">this is bad</span>',
 b'<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 dynamic HTML pages cleaner, easier, safer and intuitive in Python.

What about performance?

Although performance is not the primary goal here, it should not be a roadblock. htmldoom is copying the syntax and some of the rendering 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 believe can be implemented in Python to a great extent.
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 should perform really well.
Also since it's all Python, the power is in your hands to make all the optimisations possible at the lowest level.

Still worried about performance. Is there any benchmark?

Basic benchmarks are done and it shows that htmldoom performs better than traditional rendering engines without explicitly making any optimisation.
Refer to the benchmarks here.

Plugins and ecosystem

  • moodlmth: Convert raw HTML pages into python source code

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.6.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

htmldoom-0.6.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: htmldoom-0.6.2.tar.gz
  • Upload date:
  • Size: 10.1 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.6.2.tar.gz
Algorithm Hash digest
SHA256 a2532b86549f45c5adef43fe5e23b0308e468550dd7f002a20902d0359b8efa4
MD5 f29087ddae87ee7e159a5869e30a169e
BLAKE2b-256 a908e821454670c7831510dc813c97d88a3b90f98e0bbabf644af95d3572378e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: htmldoom-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 11.3 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.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b2bf740498a285d36aa4533d936c5c5e892d5874c90f925ecc5355ee8f250b1
MD5 71084f3e5e7bd72eb4cb73323e23a1cc
BLAKE2b-256 f57923c6103b8f61f5209dc3e65b2604b03b32e8aa16ddaab580b1dc6e4618b3

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