Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/niklasf/python-tinyhtml

A tiny library to safely render compact HTML5 from Python expressions.
https://github.com/niklasf/python-tinyhtml

functional-programming html html5 python python3 vdom

Last synced: 4 days ago
JSON representation

A tiny library to safely render compact HTML5 from Python expressions.

Awesome Lists containing this project

README

        

tinyhtml
========

A tiny library to safely render compact HTML5 from Python expressions.

.. image:: https://github.com/niklasf/python-tinyhtml/workflows/Test/badge.svg
:target: https://github.com/niklasf/python-tinyhtml/actions
:alt: Test status

.. image:: https://badge.fury.io/py/tinyhtml.svg
:target: https://pypi.python.org/pypi/tinyhtml
:alt: PyPI package

Introduction
------------

This is the entire API. The following documentation is longer than the
implementation.

.. code:: python

>>> from tinyhtml import html, h, frag, raw

The most important function is ``h()``. Below you see how to render attributes,
normal elements, and void/self-closing elements.

.. code:: python

>>> html(lang="en")(
... h("head")(
... h("meta", charset="utf-8"),
... ),
... ).render()
''

Use ``frag()`` to pass around groups of elements.

.. code:: python

>>> frag(
... h("h1")("Lorem ipsum ..."),
... h("p")("... dolor sit amet."),
... )
raw('

Lorem ipsum ...

... dolor sit amet.

')

Of course all content and attributes are properly escaped. Use ``raw()`` as an
escape hatch to render unescaped HTML.

.. code:: python

>>> print(h("a", title="&<>\"'")("&<>\"'").render())
&<>"'

>>> print(raw("