Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keithasaurus/simple_html
fast, templateless html generation
https://github.com/keithasaurus/simple_html
html python typesafe
Last synced: about 1 month ago
JSON representation
fast, templateless html generation
- Host: GitHub
- URL: https://github.com/keithasaurus/simple_html
- Owner: keithasaurus
- License: mit
- Created: 2019-10-05T07:20:45.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-24T06:09:28.000Z (8 months ago)
- Last Synced: 2024-04-25T05:21:38.085Z (7 months ago)
- Topics: html, python, typesafe
- Language: Python
- Homepage:
- Size: 196 KB
- Stars: 32
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-html - keithasaurus/simple_html
- awesome-python-html - keithasaurus/simple_html
README
# 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, pnode = div({},
h1({"id": "hello"},
"Hello World!"),
p({},
"hooray!"))render(node)
#Hello World!
hooray!
```There are several ways to render nodes:
```python
from simple_html import br, div, h1, img, render# raw node
render(br)
## node with attributes only
render(img({"src": "/some/image/url.jpg", "alt": "a great picture"}))
## node with children
render(
div({},
h1({},
"something"))
)
#'something
```Tag attributes with `None` as the value will only render the attribute name:
```python
from simple_html import div, renderrender(
div({"empty-str-attribute": "",
"key-only-attr": None})
)
#
```You can render inline css styles with `render_styles`:
```python
from simple_html import div, render, render_stylesstyles = render_styles({"min-width": "25px"})
render(
div({"style": styles},
"cool")
)
#cool# ints and floats are legal values
styles = render_styles({"padding": 0, "flex-grow": 0.6})render(
div({"style": styles},
"wow")
)
#wow
```Lists and generators are both valid collections of nodes:
```python
from typing import Generator
from simple_html import div, render, Node, brdef get_list_of_nodes() -> list[Node]:
return ["neat", br]render(div({}, get_list_of_nodes()))
#neat
def node_generator() -> Generator[Node, None, None]:
yield "neat"
yield brrender(
div({}, node_generator())
)
#neat
```For convenience, many tags are provided, but you can also create your own:
```python
from simple_html import Tag, rendercustom_elem = Tag("custom-elem")
# works the same as any other tag
node = custom_elem(
{"id": "some-custom-elem-id"},
"Wow"
)render(node) # Wow
```Strings are escaped by default, but you can pass in `SafeString`s to avoid escaping.
```python
from simple_html import br, p, SafeString, rendernode = p({},
"Escaped & stuff",
br,
SafeString("Not escaped & stuff"))render(node) #
Escaped & stuff
Not escaped & stuff
```Attributes are also escaped -- both names and values. You can use `SafeString` to bypass, if needed.
```python
from simple_html import div, render, SafeStringescaped_attrs_node = div({"":""})
render(escaped_attrs_node) #
unescaped_attrs_node = div({SafeString(""): SafeString("")})
render(unescaped_attrs_node) #
="">
```