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 custom tag

>>> from htmldoom import render, composite_tag
>>> 
>>> clipboard_copy = composite_tag("clipboard-copy")
>>> print(render(clipboard_copy(value="foo")("Copy Me")))
<clipboard-copy value="foo">Copy Me</clipboard-copy>

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 might surpass the performance of traditional template rendering engines.

WARNING: It performs a "{rendered_elements}".format(**returned_data). So each `{` or `}` in the compile-time code needs to be escaped with `{{` or `}}`.

A functional style foreach loop with a switch case (probably useless)

>>> 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 uses pure functions with hashable input parameters as elements. Hence, it makes effective use of caching internally. It also offers a friendly mechanism to pre-render the static parts of the page using the @renders decorator when it compiles to bytecode.
Also since it helps you (probably forces you) to refactor the webpage into multiple render functions, you are free to use whatever optimisation you prefer. Try putting an @lru_cache in a render function?

Is there any benchmark?

Refer to the benchmarks here.

Plugins and ecosystem

  • moodlmth: Convert raw HTML pages into python source code

Contributing

Check out the contributing guidelines.

NOTE: This file was generated using this script.

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

Uploaded Source

Built Distribution

htmldoom-0.6.6-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: htmldoom-0.6.6.tar.gz
  • Upload date:
  • Size: 10.2 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.6.tar.gz
Algorithm Hash digest
SHA256 d17d2d0a3e07607eb9f0a34661d0be4da972abbf229a02fdf0e049ce86789c0c
MD5 335a87e78a9ac4713a6a48a2dbc41f03
BLAKE2b-256 eb7f0f5b340f1d50a2bd6cf8ceac22d2edcdf0b3d35bdd758b54021cb86e650f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: htmldoom-0.6.6-py3-none-any.whl
  • Upload date:
  • Size: 11.4 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0d864194224d703ebccb177e4a7f74de0946ea4b679b03029ac6a19a2107b3b0
MD5 2d01b30abc3fc95d4e7abe55217504d6
BLAKE2b-256 048bd9149e60cf1f0da029c1a4a30e7fa55012a2086c383cd6f9cd07b1e8a3d5

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