# htbuilder — tiny HTML string builder for Python
htbuilder lets you build HTML strings using a purely functional syntax in Python.
Why use templating languages when you can just use functions?
(PS: If you like this, check out [jsbuilder](https://github.com/tvst/jsbuilder) which
lets you build JavaScript strings by simply annotating Python functions!)
## Installation
Just PIP it!
```py
pip install htbuilder
```
## Usage
Just import tags like `div` with `from htbuilder import div`, then call them:
```py
# Import any tag you want from htbuilder, and it just works!
# (This syntax requires Python 3.7+. See below for an alternate syntax)
from htbuilder import div
dom = div('Hello world!')
```
Then you can get the string output by calling `str()` on it:
```py
str(dom)
# Returns '<div>Hello world!</div>'
```
...which means you can also just `print()` to see it in the terminal:
```py
print(dom)
# Prints '<div>Hello world!</div>'
```
To specify attributes, call the tag builder with keyword args:
```py
print(
div(id='sidebar', foo='bar')
)
# Prints '<div id="sidebar" foo="bar"></div>'
```
To specify both attributes and children, first specify the attributes using
keyword args, then pass the children afterwards **inside a new
set of parentheses**:
```py
print(
div(id='sidebar', foo='bar')(
"Hello world!"
)
)
# Prints '<div id="sidebar" foo="bar">Hello world!</div>'
```
This is required because Python doesn't allow you to pass keyword arguments
_before_ you pass normal arguments.
## Multiple children
Want to output multiple children? Just pass them all as arguments:
```py
from htbuilder import div, ul, li, img
dom = (
div(id='container')(
ul(_class='greetings')(
li('hello'),
li('hi'),
li('whattup'),
)
)
)
print(dom)
# Prints this (but without added spacing):
# <div id="container">
# <ul class="greetings">
# <li>hello</li>
# <li>hi</li>
# <li>whattup</li>
# </ul>
# </div>
```
## Programmatically add children
You can also pass any iterable to specify multiple children, which means you can
simply use things like generator expressions for great awesome:
```py
from htbuilder import div, ul, li, img
image_paths = [
'http://myimages.com/foo1.jpg',
'http://myimages.com/foo2.jpg',
'http://myimages.com/foo3.jpg',
]
dom = (
div(id='container')(
ul(_class='image-list')(
li(img(src=image_path, _class='large-image'))
for image_path in image_paths
)
)
)
print(dom)
# Prints:
# <div id="container">
# <ul class="image-list">
# <li><img src="http://myimages.com/foo1.jpg" class="large-image"/></li>
# <li><img src="http://myimages.com/foo2.jpg" class="large-image"/></li>
# <li><img src="http://myimages.com/foo3.jpg" class="large-image"/></li>
# </ul>
# </div>
```
## Conditionally add elements
And because it's just Python, you can use an if/else expression to conditionally
insert elements:
```py
use_bold = True
dom = (
div(
b("bold text")
if use_bold else
"normal text"
)
)
print(dom)
# Prints: <div><b>bold text</b></div>
```
## Styling
We provide helpers to write styles without having to pass huge style strings as
arguments. Instead, just use handy builders like `styles()`, `classes()`,
`fonts()`, along with helpers you can import from the `units` and `funcs`
modules.
```py
# styles, classes, and fonts are special imports to help build attribute strings.
from htbuilder import div, styles, classes, fonts
# You can import anything from .units and .funcs to make it easier to specify
# units like "%" and "px", as well as functions like "rgba()" and "rgba()".
from htbuilder.units import percent, px
from htbuilder.funcs import rgba, rgb
bottom_margin = 10
is_big = True
dom = (
div(
_class=classes('btn', big=is_big)
style=styles(
color='black',
font_family=fonts('Comic Sans', 'sans-serif'),
margin=px(0, 0, bottom_margin, 0),
padding=(px(10), percent(5))
box_shadow=[
(0, 0, px(10), rgba(0, 0, 0, 0.1)),
(0, 0, '2px', rgb(0, 0, 0)),
],
)
)
)
# Prints:
# <div
# class="btn big"
# style="
# color: black;
# font-family: "Comic Sans", "sans-serif";
# margin: 0 0 10px 0;
# padding: 10px 5%;
# box-shadow: 0 0 10px rgba(0, 0, 0, 0.1), 0 0 2px rgb(0, 0, 0);
# "></div>
```
## Underscores are magic
### Use underscores instead of dashes
Like most popular languages, Python doesn't support dashes in identifiers. So if you want to build
an element that includes dashes in the tag name or attributes, like `<my-element foo-bar="baz">`, you can
do so by using underscores instead:
```py
from htbuilder import my_element
dom = my_element(foo_bar="baz")
print(dom)
# Prints:
# <my-element foo-bar="baz"></my-element>
```
### Prefix with underscore to avoid reserved words
The word `class` is reserved in Python, so if you want to set an element's `class` attribute you
should prepend it with an underscore like this:
```py
dom = div(_class="myclass")
print(dom)
# Prints:
# <div class="myclass"></div>
```
This works because underscores preceding or following any identifier are automatically stripped away
for you.
## Working with Python < 3.7
If using Python < 3.7, the import should look like this instead:
```py
from htbuilder import H
div = H.div
ul = H.ul
li = H.li
img = H.img
# ...etc
```
Raw data
{
"_id": null,
"home_page": "https://github.com/tvst/htbuilder",
"name": "htbuilder",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": null,
"keywords": null,
"author": "Thiago Teixeira",
"author_email": "me@thiagot.com",
"download_url": "https://files.pythonhosted.org/packages/84/98/bca8c1e1ad2ff46cd07931a36b126ff8100bb382729d3fc25d79c0ff40ce/htbuilder-0.7.0.tar.gz",
"platform": null,
"description": "# htbuilder \u2014 tiny HTML string builder for Python\n\nhtbuilder lets you build HTML strings using a purely functional syntax in Python.\nWhy use templating languages when you can just use functions?\n\n(PS: If you like this, check out [jsbuilder](https://github.com/tvst/jsbuilder) which\nlets you build JavaScript strings by simply annotating Python functions!)\n\n## Installation\n\nJust PIP it!\n\n```py\npip install htbuilder\n```\n\n## Usage\n\nJust import tags like `div` with `from htbuilder import div`, then call them:\n\n```py\n# Import any tag you want from htbuilder, and it just works!\n# (This syntax requires Python 3.7+. See below for an alternate syntax)\nfrom htbuilder import div\n\ndom = div('Hello world!')\n```\n\nThen you can get the string output by calling `str()` on it:\n\n```py\nstr(dom)\n# Returns '<div>Hello world!</div>'\n```\n\n...which means you can also just `print()` to see it in the terminal:\n\n```py\nprint(dom)\n# Prints '<div>Hello world!</div>'\n```\n\nTo specify attributes, call the tag builder with keyword args:\n\n```py\nprint(\n div(id='sidebar', foo='bar')\n)\n# Prints '<div id=\"sidebar\" foo=\"bar\"></div>'\n```\n\nTo specify both attributes and children, first specify the attributes using\nkeyword args, then pass the children afterwards **inside a new\nset of parentheses**:\n\n```py\nprint(\n div(id='sidebar', foo='bar')(\n \"Hello world!\"\n )\n)\n# Prints '<div id=\"sidebar\" foo=\"bar\">Hello world!</div>'\n```\n\nThis is required because Python doesn't allow you to pass keyword arguments\n_before_ you pass normal arguments.\n\n\n## Multiple children\n\nWant to output multiple children? Just pass them all as arguments:\n\n```py\nfrom htbuilder import div, ul, li, img\n\ndom = (\n div(id='container')(\n ul(_class='greetings')(\n li('hello'),\n li('hi'),\n li('whattup'),\n )\n )\n)\n\nprint(dom)\n\n# Prints this (but without added spacing):\n# <div id=\"container\">\n# <ul class=\"greetings\">\n# <li>hello</li>\n# <li>hi</li>\n# <li>whattup</li>\n# </ul>\n# </div>\n```\n\n## Programmatically add children\n\nYou can also pass any iterable to specify multiple children, which means you can\nsimply use things like generator expressions for great awesome:\n\n```py\nfrom htbuilder import div, ul, li, img\n\nimage_paths = [\n 'http://myimages.com/foo1.jpg',\n 'http://myimages.com/foo2.jpg',\n 'http://myimages.com/foo3.jpg',\n]\n\ndom = (\n div(id='container')(\n ul(_class='image-list')(\n li(img(src=image_path, _class='large-image'))\n for image_path in image_paths\n )\n )\n)\n\nprint(dom)\n# Prints:\n# <div id=\"container\">\n# <ul class=\"image-list\">\n# <li><img src=\"http://myimages.com/foo1.jpg\" class=\"large-image\"/></li>\n# <li><img src=\"http://myimages.com/foo2.jpg\" class=\"large-image\"/></li>\n# <li><img src=\"http://myimages.com/foo3.jpg\" class=\"large-image\"/></li>\n# </ul>\n# </div>\n```\n\n## Conditionally add elements\n\nAnd because it's just Python, you can use an if/else expression to conditionally\ninsert elements:\n\n```py\nuse_bold = True\n\ndom = (\n div(\n b(\"bold text\")\n if use_bold else\n \"normal text\"\n )\n)\n\nprint(dom)\n# Prints: <div><b>bold text</b></div>\n```\n\n## Styling\n\nWe provide helpers to write styles without having to pass huge style strings as\narguments. Instead, just use handy builders like `styles()`, `classes()`,\n`fonts()`, along with helpers you can import from the `units` and `funcs`\nmodules.\n\n```py\n# styles, classes, and fonts are special imports to help build attribute strings.\nfrom htbuilder import div, styles, classes, fonts\n\n# You can import anything from .units and .funcs to make it easier to specify\n# units like \"%\" and \"px\", as well as functions like \"rgba()\" and \"rgba()\".\nfrom htbuilder.units import percent, px\nfrom htbuilder.funcs import rgba, rgb\n\nbottom_margin = 10\nis_big = True\n\ndom = (\n div(\n _class=classes('btn', big=is_big)\n style=styles(\n color='black',\n font_family=fonts('Comic Sans', 'sans-serif'),\n margin=px(0, 0, bottom_margin, 0),\n padding=(px(10), percent(5))\n box_shadow=[\n (0, 0, px(10), rgba(0, 0, 0, 0.1)),\n (0, 0, '2px', rgb(0, 0, 0)),\n ],\n )\n )\n)\n\n# Prints:\n# <div\n# class=\"btn big\"\n# style=\"\n# color: black;\n# font-family: \"Comic Sans\", \"sans-serif\";\n# margin: 0 0 10px 0;\n# padding: 10px 5%;\n# box-shadow: 0 0 10px rgba(0, 0, 0, 0.1), 0 0 2px rgb(0, 0, 0);\n# \"></div>\n```\n\n\n## Underscores are magic\n\n### Use underscores instead of dashes\n\nLike most popular languages, Python doesn't support dashes in identifiers. So if you want to build\nan element that includes dashes in the tag name or attributes, like `<my-element foo-bar=\"baz\">`, you can\ndo so by using underscores instead:\n\n```py\nfrom htbuilder import my_element\n\ndom = my_element(foo_bar=\"baz\")\n\nprint(dom)\n# Prints:\n# <my-element foo-bar=\"baz\"></my-element>\n```\n\n### Prefix with underscore to avoid reserved words\n\nThe word `class` is reserved in Python, so if you want to set an element's `class` attribute you\nshould prepend it with an underscore like this:\n\n```py\ndom = div(_class=\"myclass\")\n\nprint(dom)\n# Prints:\n# <div class=\"myclass\"></div>\n```\n\nThis works because underscores preceding or following any identifier are automatically stripped away\nfor you.\n\n\n## Working with Python < 3.7\n\nIf using Python < 3.7, the import should look like this instead:\n\n```py\nfrom htbuilder import H\n\ndiv = H.div\nul = H.ul\nli = H.li\nimg = H.img\n# ...etc\n```\n",
"bugtrack_url": null,
"license": "Apache 2",
"summary": "A purely-functional HTML builder for Python. Think JSX rather than templates.",
"version": "0.7.0",
"project_urls": {
"Homepage": "https://github.com/tvst/htbuilder"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8498bca8c1e1ad2ff46cd07931a36b126ff8100bb382729d3fc25d79c0ff40ce",
"md5": "8fae5a74ef2aa122deac43ef99758ab3",
"sha256": "61435d649438d5b7c3d30868f5644b83f0ab488d411eb0485439cb9e05223f19"
},
"downloads": -1,
"filename": "htbuilder-0.7.0.tar.gz",
"has_sig": false,
"md5_digest": "8fae5a74ef2aa122deac43ef99758ab3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 10401,
"upload_time": "2024-11-15T08:53:18",
"upload_time_iso_8601": "2024-11-15T08:53:18.085949Z",
"url": "https://files.pythonhosted.org/packages/84/98/bca8c1e1ad2ff46cd07931a36b126ff8100bb382729d3fc25d79c0ff40ce/htbuilder-0.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-15 08:53:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tvst",
"github_project": "htbuilder",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "htbuilder"
}