**fast_html** is a fast, minimalist HTML generator.
It is an alternative to templating engines, like Jinja,
for use with, e.g., [htmx](https://htmx.org/).
**Pros:**
- use familiar python syntax
- use efficient concatenation techniques
- optional automatic indentation
Unlike other HTML generators (e.g. [Dominate](https://pypi.org/project/dominate/)) that use python objects to represent HTML snippets,
fast_html represents HTML snippets using string [generators](https://docs.python.org/3/glossary.html#term-generator)
that can be rendered extremely fast using `join`.
(see [here](https://python.plainenglish.io/concatenating-strings-efficiently-in-python-9bfc8e8d6f6e))
**Like other HTML generators, one needs to remember:**
- the name of some tags and attributes is changed (e.g., `class_` instead of `class`, due to Python parser)
- there may be conflicts of function names with your code base
Installation
============
`pip install fast_html` or copy the (single) source file in your project.
Don't forget to `add a star on GitHub <https://github.com/pcarbonn/fast_html>`_ ! Thanks.
Tutorial:
=========
>>> from fast_html import *
A tag is created by calling a function of the corresponding name,
and rendered using `render`:
>>> print(render(p("text")))
<p>text</p>
Tag attributes are specified using named arguments:
>>> print(render(br(id=1)))
<br id="1">
>>> print(render(br(id=None)))
<br>
>>> print(render(ul(li("text", selected=True))))
<ul><li selected>text</li></ul>
>>> print(render(ul(li("text", selected=False))))
<ul><li>text</li></ul>
The python parser introduces some constraints:
- The following tags require a trailing underscore: `del_`, `input_`, `map_`, `object_`.
- The following tag attributes require a trailing underscore: `class_`, `for_`.
In fact, the trailing underscore in attribute names is always removed by fast_html,
and other underscores are replaced by `-`.
For example, the htmx attribute `hx-get` is set using `hx_get="url"`.
>>> print(render(object_("text", class_="s12", hx_get="url")))
<object class="s12" hx-get="url">text</object>
>>> print(render(button("Click me", hx_post="/clicked", hx_swap="outerHTML")))
<button hx-post="/clicked" hx-swap="outerHTML">Click me</button>
The innerHTML can be a list:
>>> print(render(div(["text", span("item 1"), span("item 2")])))
<div>text<span>item 1</span><span>item 2</span></div>
The innerHTML can also be a list of lists:
>>> print(render(div(["text", [span(f"item {i}") for i in [1,2]]])))
<div>text<span>item 1</span><span>item 2</span></div>
>>> print(render([br(), br()]))
<br><br>
The innerHTML can also be specified using the `i` parameter,
after the other attributes, to match the order of rendering:
>>> print(render(ul(class_="s12", i=[
... li("item 1"),
... li("item 2")]
... )))
<ul class="s12"><li>item 1</li><li>item 2</li></ul>
You can create your own tag using the `tag` function:
>>> def my_tag(inner=None, **kwargs):
... yield from tag("my_tag", inner, **kwargs)
>>> print(render(my_tag("text")))
<my_tag>text</my_tag>
Options:
========
By default, the inner string of a tag is not escaped:
characters `&`, `<` and `>` in it are not converted to HTML-safe sequences.
>>> print(render(p("<bold>text</bold>")))
<p><bold>text</bold></p>
Of course, you can escape strings before calling fast_html:
>>> from html import escape
>>> print(render(p(escape("<bold>text</bold>"))))
<p><bold>text</bold></p>
If your policy is to escape every inner string,
you can activate escaping by setting the variable `escape` to `False`
(or by calling `escape_it(False)`).
>>> escape_it(True)
>>> print(render(p("<bold>text</bold>")))
<p><bold>text</bold></p>
When debugging your code, you can set global variable `indent` to `True`
(or call `indent_it(True)`) to obtain HTML with tag indentation, e.g.,
>>> indent_it(True)
>>> print(render(div(class_="s12", i=["text\n", span("item 1"), span("item 2")])))
<div class="s12">
text
<span>
item 1
</span>
<span>
item 2
</span>
</div>
<BLANKLINE>
You can also convert an HTML string to a function-based code representation:
>>> print(html_to_code('<div class="example"><p>Some text</p></div>'))
[div([p(['Some text'], )], class_="example")]
Raw data
{
"_id": null,
"home_page": "https://github.com/pcarbonn/fast_html",
"name": "fast-html",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.7",
"maintainer_email": null,
"keywords": "HTML, HTMX",
"author": "Pierre",
"author_email": "pierre.carbonnelle@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/92/69/445335af37fd99077956a5658dd9c1fac587169a1c6ec102d26e5ec52b79/fast_html-1.0.9.tar.gz",
"platform": null,
"description": "**fast_html** is a fast, minimalist HTML generator.\n\n\nIt is an alternative to templating engines, like Jinja,\nfor use with, e.g., [htmx](https://htmx.org/).\n\n**Pros:**\n\n- use familiar python syntax\n\n- use efficient concatenation techniques\n\n- optional automatic indentation\n\nUnlike other HTML generators (e.g. [Dominate](https://pypi.org/project/dominate/)) that use python objects to represent HTML snippets,\nfast_html represents HTML snippets using string [generators](https://docs.python.org/3/glossary.html#term-generator)\nthat can be rendered extremely fast using `join`.\n(see [here](https://python.plainenglish.io/concatenating-strings-efficiently-in-python-9bfc8e8d6f6e))\n\n**Like other HTML generators, one needs to remember:**\n\n- the name of some tags and attributes is changed (e.g., `class_` instead of `class`, due to Python parser)\n\n- there may be conflicts of function names with your code base\n\n\nInstallation\n============\n`pip install fast_html` or copy the (single) source file in your project.\n\nDon't forget to `add a star on GitHub <https://github.com/pcarbonn/fast_html>`_ ! Thanks.\n\n\nTutorial:\n=========\n\n >>> from fast_html import *\n\nA tag is created by calling a function of the corresponding name,\nand rendered using `render`:\n\n >>> print(render(p(\"text\")))\n <p>text</p>\n\nTag attributes are specified using named arguments:\n\n >>> print(render(br(id=1)))\n <br id=\"1\">\n\n >>> print(render(br(id=None)))\n <br>\n\n >>> print(render(ul(li(\"text\", selected=True))))\n <ul><li selected>text</li></ul>\n\n >>> print(render(ul(li(\"text\", selected=False))))\n <ul><li>text</li></ul>\n\nThe python parser introduces some constraints:\n\n- The following tags require a trailing underscore: `del_`, `input_`, `map_`, `object_`.\n\n- The following tag attributes require a trailing underscore: `class_`, `for_`.\n\nIn fact, the trailing underscore in attribute names is always removed by fast_html,\nand other underscores are replaced by `-`.\nFor example, the htmx attribute `hx-get` is set using `hx_get=\"url\"`.\n\n >>> print(render(object_(\"text\", class_=\"s12\", hx_get=\"url\")))\n <object class=\"s12\" hx-get=\"url\">text</object>\n\n >>> print(render(button(\"Click me\", hx_post=\"/clicked\", hx_swap=\"outerHTML\")))\n <button hx-post=\"/clicked\" hx-swap=\"outerHTML\">Click me</button>\n\nThe innerHTML can be a list:\n\n >>> print(render(div([\"text\", span(\"item 1\"), span(\"item 2\")])))\n <div>text<span>item 1</span><span>item 2</span></div>\n\nThe innerHTML can also be a list of lists:\n\n >>> print(render(div([\"text\", [span(f\"item {i}\") for i in [1,2]]])))\n <div>text<span>item 1</span><span>item 2</span></div>\n\n >>> print(render([br(), br()]))\n <br><br>\n\nThe innerHTML can also be specified using the `i` parameter,\nafter the other attributes, to match the order of rendering:\n\n >>> print(render(ul(class_=\"s12\", i=[\n ... li(\"item 1\"),\n ... li(\"item 2\")]\n ... )))\n <ul class=\"s12\"><li>item 1</li><li>item 2</li></ul>\n\nYou can create your own tag using the `tag` function:\n\n >>> def my_tag(inner=None, **kwargs):\n ... yield from tag(\"my_tag\", inner, **kwargs)\n >>> print(render(my_tag(\"text\")))\n <my_tag>text</my_tag>\n\n\nOptions:\n========\n\nBy default, the inner string of a tag is not escaped:\ncharacters `&`, `<` and `>` in it are not converted to HTML-safe sequences.\n\n\n >>> print(render(p(\"<bold>text</bold>\")))\n <p><bold>text</bold></p>\n\nOf course, you can escape strings before calling fast_html:\n\n\n >>> from html import escape\n >>> print(render(p(escape(\"<bold>text</bold>\"))))\n <p><bold>text</bold></p>\n\nIf your policy is to escape every inner string,\nyou can activate escaping by setting the variable `escape` to `False`\n(or by calling `escape_it(False)`).\n\n >>> escape_it(True)\n >>> print(render(p(\"<bold>text</bold>\")))\n <p><bold>text</bold></p>\n\nWhen debugging your code, you can set global variable `indent` to `True`\n(or call `indent_it(True)`) to obtain HTML with tag indentation, e.g.,\n\n >>> indent_it(True)\n >>> print(render(div(class_=\"s12\", i=[\"text\\n\", span(\"item 1\"), span(\"item 2\")])))\n <div class=\"s12\">\n text\n <span>\n item 1\n </span>\n <span>\n item 2\n </span>\n </div>\n <BLANKLINE>\n\nYou can also convert an HTML string to a function-based code representation:\n\n >>> print(html_to_code('<div class=\"example\"><p>Some text</p></div>'))\n [div([p(['Some text'], )], class_=\"example\")]\n\n",
"bugtrack_url": null,
"license": "LGPL-3.0-or-later",
"summary": "A fast, minimalist HTML generator",
"version": "1.0.9",
"project_urls": {
"Homepage": "https://github.com/pcarbonn/fast_html",
"Repository": "https://github.com/pcarbonn/fast_html"
},
"split_keywords": [
"html",
" htmx"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6dcbadb3e2b1477a9db5c626f7beac4f9cf0c9a057520e2ed2cf4fe3ec5c44db",
"md5": "475258737ab82bb12fd3e4426d4e1b6d",
"sha256": "57fcbb99f7ad24650845433351a88beda26c98e36aededc4ee42598ca29dd6f6"
},
"downloads": -1,
"filename": "fast_html-1.0.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "475258737ab82bb12fd3e4426d4e1b6d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.7",
"size": 8942,
"upload_time": "2024-04-02T05:49:13",
"upload_time_iso_8601": "2024-04-02T05:49:13.939675Z",
"url": "https://files.pythonhosted.org/packages/6d/cb/adb3e2b1477a9db5c626f7beac4f9cf0c9a057520e2ed2cf4fe3ec5c44db/fast_html-1.0.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9269445335af37fd99077956a5658dd9c1fac587169a1c6ec102d26e5ec52b79",
"md5": "fa39ec12a4119380af57dd5e4627ea11",
"sha256": "739c79a237ff5657ac47eec4e1cb2a85291c530fcf544c3a98ad7614c7b3c471"
},
"downloads": -1,
"filename": "fast_html-1.0.9.tar.gz",
"has_sig": false,
"md5_digest": "fa39ec12a4119380af57dd5e4627ea11",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.7",
"size": 8031,
"upload_time": "2024-04-02T05:49:15",
"upload_time_iso_8601": "2024-04-02T05:49:15.772153Z",
"url": "https://files.pythonhosted.org/packages/92/69/445335af37fd99077956a5658dd9c1fac587169a1c6ec102d26e5ec52b79/fast_html-1.0.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-02 05:49:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pcarbonn",
"github_project": "fast_html",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fast-html"
}