simple-html


Namesimple-html JSON
Version 1.2.2 PyPI version JSON
download
home_pagehttps://github.com/keithasaurus/simple_html
SummaryTemplate-less html rendering in Python
upload_time2024-01-02 06:10:52
maintainer
docs_urlNone
authorKeith Philpott
requires_python>=3.8.1,<4.0.0
licenseMIT
keywords html type hints
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # simple_html

### Template-less. Type-safe. Minified by default. Fast.

simple_html allows you to create HTML in standard Python. Benefits include:
- typically faster than jinja2 -- up to 15x faster
- typically renders fewer bytes than template-based rendering
- types let your editor and tools help you write correct code faster
- lightweight and framework agnostic
- always renders valid html


### Installation
`pip install simple-html`


### Usage

```python
from simple_html import div, h1, render, p

node = div({},
           h1({"id": "hello"},
              "Hello World!"),
           p({},
             "hooray!"))

render(node)  
# <div><h1 id="hello">Hello World!</h1><p>hooray!</p></div> 
```

There are several ways to render nodes:
```python
from simple_html import br, div, h1, img, render

# raw node
render(br)
# <br/>

# node with attributes only
render(img({"src": "/some/image/url.jpg", "alt": "a great picture"}))
# <img src="/some/image/url.jpg" alt="a great picture"/>

# node with children
render(
    div({},
        h1({},
           "something"))
)
# <div><h1>something</h1></div>'
```

Tag attributes with `None` as the value will only render the attribute name:
```python
from simple_html import div, render

render(
    div({"empty-str-attribute": "", 
         "key-only-attr": None})
)
# <div empty-str-attribute="" key-only-attr></div>
```

You can render inline css styles with `render_styles`:
```python
from simple_html import div, render, render_styles

styles = render_styles({"min-width": "25px"})

render(
    div({"style": styles}, 
        "cool")
)
# <div style="min-width:25px;">cool</div>


# ints and floats are legal values
styles = render_styles({"padding": 0, "flex-grow": 0.6})

render(
    div({"style": styles},
        "wow")
)
# <div style="padding:0;flex-grow:0.6;">wow</div>
```


Lists and generators are both valid collections of nodes:
```python
from typing import Generator
from simple_html import div, render, Node, br


def get_list_of_nodes() -> list[Node]:
    return ["neat", br]


render(div({}, get_list_of_nodes()))
# <div>neat<br/></div>


def node_generator() -> Generator[Node, None, None]:
    yield "neat"
    yield br


render(
    div({}, node_generator())
)
# <div>neat<br/></div>
```


For convenience, many tags are provided, but you can also create your own:

```python
from simple_html import Tag, render

custom_elem = Tag("custom-elem")

# works the same as any other tag
node = custom_elem(
    {"id": "some-custom-elem-id"},
    "Wow"
)

render(node)  # <custom-elem id="some-custom-elem-id">Wow</custom-elem>
```


Strings are escaped by default, but you can pass in `SafeString`s to avoid escaping.

```python
from simple_html import br, p, SafeString, render

node = p({},
         "Escaped & stuff",
         br,
         SafeString("Not escaped & stuff"))

render(node)  # <p>Escaped &amp; stuff<br/>Not escaped & stuff</p> 
```

Attributes are also escaped -- both names and values. You can use `SafeString` to bypass, if needed.

```python
from simple_html import div, render, SafeString

escaped_attrs_node = div({"<bad>":"</also bad>"})

render(escaped_attrs_node)  # <div &amp;lt;bad&amp;gt;="&amp;lt;/also bad&amp;gt;"></div>

unescaped_attrs_node = div({SafeString("<bad>"): SafeString("</also bad>")})

render(unescaped_attrs_node)  # <div <bad>="</also bad>"></div>
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/keithasaurus/simple_html",
    "name": "simple-html",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.1,<4.0.0",
    "maintainer_email": "",
    "keywords": "html,type hints",
    "author": "Keith Philpott",
    "author_email": "fakekeith@example.org",
    "download_url": "https://files.pythonhosted.org/packages/b1/3e/5e3bb134a1b2d195898d5e3fbf6a8de7d6e372b346b165d53ba9e1d4d802/simple_html-1.2.2.tar.gz",
    "platform": null,
    "description": "# simple_html\n\n### Template-less. Type-safe. Minified by default. Fast.\n\nsimple_html allows you to create HTML in standard Python. Benefits include:\n- typically faster than jinja2 -- up to 15x faster\n- typically renders fewer bytes than template-based rendering\n- types let your editor and tools help you write correct code faster\n- lightweight and framework agnostic\n- always renders valid html\n\n\n### Installation\n`pip install simple-html`\n\n\n### Usage\n\n```python\nfrom simple_html import div, h1, render, p\n\nnode = div({},\n           h1({\"id\": \"hello\"},\n              \"Hello World!\"),\n           p({},\n             \"hooray!\"))\n\nrender(node)  \n# <div><h1 id=\"hello\">Hello World!</h1><p>hooray!</p></div> \n```\n\nThere are several ways to render nodes:\n```python\nfrom simple_html import br, div, h1, img, render\n\n# raw node\nrender(br)\n# <br/>\n\n# node with attributes only\nrender(img({\"src\": \"/some/image/url.jpg\", \"alt\": \"a great picture\"}))\n# <img src=\"/some/image/url.jpg\" alt=\"a great picture\"/>\n\n# node with children\nrender(\n    div({},\n        h1({},\n           \"something\"))\n)\n# <div><h1>something</h1></div>'\n```\n\nTag attributes with `None` as the value will only render the attribute name:\n```python\nfrom simple_html import div, render\n\nrender(\n    div({\"empty-str-attribute\": \"\", \n         \"key-only-attr\": None})\n)\n# <div empty-str-attribute=\"\" key-only-attr></div>\n```\n\nYou can render inline css styles with `render_styles`:\n```python\nfrom simple_html import div, render, render_styles\n\nstyles = render_styles({\"min-width\": \"25px\"})\n\nrender(\n    div({\"style\": styles}, \n        \"cool\")\n)\n# <div style=\"min-width:25px;\">cool</div>\n\n\n# ints and floats are legal values\nstyles = render_styles({\"padding\": 0, \"flex-grow\": 0.6})\n\nrender(\n    div({\"style\": styles},\n        \"wow\")\n)\n# <div style=\"padding:0;flex-grow:0.6;\">wow</div>\n```\n\n\nLists and generators are both valid collections of nodes:\n```python\nfrom typing import Generator\nfrom simple_html import div, render, Node, br\n\n\ndef get_list_of_nodes() -> list[Node]:\n    return [\"neat\", br]\n\n\nrender(div({}, get_list_of_nodes()))\n# <div>neat<br/></div>\n\n\ndef node_generator() -> Generator[Node, None, None]:\n    yield \"neat\"\n    yield br\n\n\nrender(\n    div({}, node_generator())\n)\n# <div>neat<br/></div>\n```\n\n\nFor convenience, many tags are provided, but you can also create your own:\n\n```python\nfrom simple_html import Tag, render\n\ncustom_elem = Tag(\"custom-elem\")\n\n# works the same as any other tag\nnode = custom_elem(\n    {\"id\": \"some-custom-elem-id\"},\n    \"Wow\"\n)\n\nrender(node)  # <custom-elem id=\"some-custom-elem-id\">Wow</custom-elem>\n```\n\n\nStrings are escaped by default, but you can pass in `SafeString`s to avoid escaping.\n\n```python\nfrom simple_html import br, p, SafeString, render\n\nnode = p({},\n         \"Escaped & stuff\",\n         br,\n         SafeString(\"Not escaped & stuff\"))\n\nrender(node)  # <p>Escaped &amp; stuff<br/>Not escaped & stuff</p> \n```\n\nAttributes are also escaped -- both names and values. You can use `SafeString` to bypass, if needed.\n\n```python\nfrom simple_html import div, render, SafeString\n\nescaped_attrs_node = div({\"<bad>\":\"</also bad>\"})\n\nrender(escaped_attrs_node)  # <div &amp;lt;bad&amp;gt;=\"&amp;lt;/also bad&amp;gt;\"></div>\n\nunescaped_attrs_node = div({SafeString(\"<bad>\"): SafeString(\"</also bad>\")})\n\nrender(unescaped_attrs_node)  # <div <bad>=\"</also bad>\"></div>\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Template-less html rendering in Python",
    "version": "1.2.2",
    "project_urls": {
        "Homepage": "https://github.com/keithasaurus/simple_html"
    },
    "split_keywords": [
        "html",
        "type hints"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ba1445d5fb1a7a7afb1d8fcaed21bf7be49047ccc7fd24630bbb5c46900cb214",
                "md5": "5b3815b7cd859355b503a564d8fd2480",
                "sha256": "be94b204fbd205d7d4d874e9bbcba2ace588eb6d4dd84e90f454a2ccbd245558"
            },
            "downloads": -1,
            "filename": "simple_html-1.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5b3815b7cd859355b503a564d8fd2480",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 7115,
            "upload_time": "2024-01-02T06:10:51",
            "upload_time_iso_8601": "2024-01-02T06:10:51.484516Z",
            "url": "https://files.pythonhosted.org/packages/ba/14/45d5fb1a7a7afb1d8fcaed21bf7be49047ccc7fd24630bbb5c46900cb214/simple_html-1.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b13e5e3bb134a1b2d195898d5e3fbf6a8de7d6e372b346b165d53ba9e1d4d802",
                "md5": "48748dcc8fe95ac442c6c7ea367eeb9c",
                "sha256": "8a428ec793d12562af1ea6c55f8e1c1b02dc25e0449b0245b6bdb4f3bc6ff560"
            },
            "downloads": -1,
            "filename": "simple_html-1.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "48748dcc8fe95ac442c6c7ea367eeb9c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 7033,
            "upload_time": "2024-01-02T06:10:52",
            "upload_time_iso_8601": "2024-01-02T06:10:52.699299Z",
            "url": "https://files.pythonhosted.org/packages/b1/3e/5e3bb134a1b2d195898d5e3fbf6a8de7d6e372b346b165d53ba9e1d4d802/simple_html-1.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-02 06:10:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "keithasaurus",
    "github_project": "simple_html",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "simple-html"
}
        
Elapsed time: 0.48151s