An intuitive, high performance HTML rendering framework
Project description
htmldoom
An intuitive, high performance HTML rendering framework
Usage
A basic tag
>>> from htmldoom import render, elements as e >>> >>> 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") >>> >>> 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:
Python syntax
>>> from htmldoom import render, renders, elements as e >>> >>> @renders( ... e.p()("{x}"), ... e.p()("another {x}"), ... ) ... def paras(data): ... return {"x": data["x"]} ... >>> render(paras({"x": "awesome paragraph &"})) '<p>awesome paragraph &</p><p>another awesome paragraph &</p>'
YAML Syntax
# path/to/components.yml paras: awesome: - p: - - "{x}" - p: - - Another {x}
>>> from htmldoom import render, renders >>> from htmldoom.yaml_loader import loadyaml as ly >>> >>> @renders(ly("path/to/components.yml", "paras.awesome")) ... def paras(data): ... return {"x": data["x"]} ... >>> render(paras({"x": "awesome paragraph &"})) '<p>awesome paragraph &</p><p>another awesome paragraph &</p>'
HTML Syntax (using the raw file loader)
<!-- path/to/components.html --> <p>{x}</p><p>another {x}</p>
>>> from htmldoom import renders, loadraw as lr >>> >>> @renders(lr("path/to/components.html")) ... def render(data): ... return {"x": data["x"]} ... >>> print(render_paras({"x": "awesome paragraph &"})) '<p>awesome paragraph &</p><p>another awesome paragraph &</p>'
NOTE: The code inside the `@renders` decorator is executed during the module load time. Hence it doesn't add and performance overhead at runtime.
renders( ...pre-rendered template... )( ...runtime rendering logic... )The more elements you pre-render as template, the faster it gets at runtime.
WARNING: It performs a "{rendered_elements}".format(**returned_data)
. So each `{` or `}` in the pre-rendered template needs to be escaped with `{{` or `}}`. If you are using file loaders such as `loadraw()`, `loadtxt()`, `loadyaml()` etc, consider passing `static=True` in them to auto escape the curly braces.
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>')
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's been given a very high priority. 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 and reuse it.
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?
Plugins and ecosystem
- moodlmth: Convert raw HTML pages into python source code
- Flask-Htmldoom: htmldoom integration for Flask
- pyramid_htmldoom: htmldoom rendering library plugin for Pyramid
Contributing
Check out the contributing guidelines.
NOTE: This file was generated using this script.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file htmldoom-0.9.0.tar.gz
.
File metadata
- Download URL: htmldoom-0.9.0.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4689f3269ac04834db4e53019ccdb53324ec5d2b929073c26630d7ca23b908c |
|
MD5 | afd784aea26cd50d678f3ad210155252 |
|
BLAKE2b-256 | 5547cf51dec8cebdb06115bfe2cb65f1752ff23b4ce8128cba66befbae4bbe7e |
File details
Details for the file htmldoom-0.9.0-py3-none-any.whl
.
File metadata
- Download URL: htmldoom-0.9.0-py3-none-any.whl
- Upload date:
- Size: 14.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75e0548044ca70a02f02e464e7f0680b0bd54dd4c6c0fcdef6f7032bb0f6e493 |
|
MD5 | 880aa573d854040416efeaa9436b4e18 |
|
BLAKE2b-256 | c09100840a0276dc6d28c5fcd3a9f01ee8db219485dc3e83518ad31c8ef41f67 |