Build HTML with Python. No templates. No regrets.
╭─╴╷ ╷╷╭─╴╷╭ ╭─╴╭╮╷ ╭╮╷╷ ╷╭─╴╭─╴╶┬╴
│ ├─┤││ ├┴╮├╴ │╰┤ │╰┤│ ││╶╮├╴ │
╰─╴╵ ╵╵╰─╴╵ ╵╰─╴╵ ╵ ╵ ╵╰─╯╰─╯╰─╴ ╵
Every HTML tag is a Python object. Nest them. Chain them. Pass them to functions. Print them.
pip install chicken-nugetfrom chicken import html, head, body, div, p, h1, a, meta, title
from chicken import render_page, style
page = html(
head(
meta(charset="utf-8"),
title("My Page"),
),
body(
div(class_="container",
h1("Hello from Chicken NuGet 🐔"),
p("Build HTML without leaving Python."),
a("GitHub", href="https://github.com/360Digital/chicken-nuget"),
style=style(max_width="640px", margin="2rem auto",
font_family="Courier New, monospace"),
)
)
)
print(render_page(page))
# → <!DOCTYPE html>
# → <html>
# → <head>...Every HTML tag is a callable that accepts *children and **attrs:
div("hello") # <div>hello</div>
div(p("hi"), p("there")) # nested children
a("click", href="#", class_="btn") # attrs as kwargs
input_(type="text", disabled=True) # boolean attrs
img(src="cat.png", alt="a cat") # void elements (no closing tag)Keyword name rules:
- Trailing
_stripped:class_→class,for_→for - Internal
_→-:data_value→data-value
div().add(p("a"), p("b")) # append children, returns self
div().attr(id="box", class_="main") # set attrs, returns selfConverts Python kwargs to an inline CSS string:
style(color="red", font_size="16px", margin_top="1rem")
# → "color: red; font-size: 16px; margin-top: 1rem;"Underscores become hyphens. Pass the result to style=:
div("hello", style=style(background="#fffbe6", padding="1rem"))render(p("hi")) # "<p>hi</p>"
render(html(...), doctype=True) # "<!DOCTYPE html>\n<html>..."
render(page, minify=True) # single-line outputShorthand for render(element, doctype=True).
to_file(page, "index.html")Inject raw HTML strings without any wrapper element:
Raw("<!-- Google Analytics -->", "<script>...</script>")from chicken import div, h2, p, a, render
def card(title, body, href=None):
children = [h2(title), p(body)]
if href:
children.append(a("Read more →", href=href))
return div(*children, class_="card")
grid = div(
card("Zero deps", "Pure Python.", "#"),
card("Composable", "Just objects.", "#"),
class_="grid"
)
print(render(grid, doctype=True))python examples/basic.py > output.html
python examples/page_builder.py > output.htmlgit clone https://github.com/360Digital/chicken-nuget
cd chicken-nuget
pip install -e ".[dev]"
pytestBecause .nuget is a package manager, chicken is delicious, and sometimes you just need to name something before you build it.
Made with ❤️ and Courier New by 360Digital, Co.