fast_html


Namefast_html JSON
Version 1.0.11 PyPI version JSON
download
home_pagehttps://github.com/pcarbonn/fast_html
SummaryA fast, minimalist HTML generator
upload_time2024-09-20 09:49:22
maintainerNone
docs_urlNone
authorPierre
requires_python<4.0,>=3.7
licenseLGPL-3.0-or-later
keywords html htmx
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            **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>&lt;bold&gt;text&lt;/bold&gt;</p>

If your policy is to escape every inner string,
you can activate escaping by setting the variable `escape` to `True`
(or by calling `escape_it(True)`).

    >>> escape_it(True)
    >>> print(render(p("<bold>text</bold>")))
    <p>&lt;bold&gt;text&lt;/bold&gt;</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/41/2b/9cc1c386fdf55d04c1f1346ec869eda28bd2da4e10f5514948b892b5f22a/fast_html-1.0.11.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>&lt;bold&gt;text&lt;/bold&gt;</p>\n\nIf your policy is to escape every inner string,\nyou can activate escaping by setting the variable `escape` to `True`\n(or by calling `escape_it(True)`).\n\n    >>> escape_it(True)\n    >>> print(render(p(\"<bold>text</bold>\")))\n    <p>&lt;bold&gt;text&lt;/bold&gt;</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.11",
    "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": "103c7059f6357f34ef95dff955f0f14b6eba10346cbb137186e4ff3f2069ecbc",
                "md5": "ebad738a9541119c22449030e92122fa",
                "sha256": "3804fa9166ac4b1b9d7ad1e8831430efd082d1657a967c2c1baa4e58cfeb53e3"
            },
            "downloads": -1,
            "filename": "fast_html-1.0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ebad738a9541119c22449030e92122fa",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.7",
            "size": 8991,
            "upload_time": "2024-09-20T09:49:21",
            "upload_time_iso_8601": "2024-09-20T09:49:21.154851Z",
            "url": "https://files.pythonhosted.org/packages/10/3c/7059f6357f34ef95dff955f0f14b6eba10346cbb137186e4ff3f2069ecbc/fast_html-1.0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "412b9cc1c386fdf55d04c1f1346ec869eda28bd2da4e10f5514948b892b5f22a",
                "md5": "714521d8ff4f4585c07a1e2c9d59fce2",
                "sha256": "6eb8ed1f569f8e08b8af68e9dcef27a3a3e6593ac01cb3bad5647dfa9e274586"
            },
            "downloads": -1,
            "filename": "fast_html-1.0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "714521d8ff4f4585c07a1e2c9d59fce2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.7",
            "size": 8128,
            "upload_time": "2024-09-20T09:49:22",
            "upload_time_iso_8601": "2024-09-20T09:49:22.571038Z",
            "url": "https://files.pythonhosted.org/packages/41/2b/9cc1c386fdf55d04c1f1346ec869eda28bd2da4e10f5514948b892b5f22a/fast_html-1.0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-20 09:49:22",
    "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"
}
        
Elapsed time: 0.51166s