Skip to main content

Simple HTML generation

Project description

Simple HTML generation

PyPI

Installation

Install the minihtml package from PyPI:

$ pip install minihtml

Examples

Create a minihtml.Html instance to produce tags. Tags stringify to HTML:

>>> from minihtml import Html
>>> h = Html()
>>> html = h.html(h.head(h.title("Hello, World!")))
>>> print(html)
<html><head><title>Hello, World!</title></head></html>

Tags are callables that accept positional arguments (children) and keyword arguments (attributes). Calls can be chained to produce code that is structured more like HTML (attributes before content):

>>> print(h.a("link title", href="/url"))
<a href="/url">link title</a>
>>> print(h.a(href="/url")("link title"))
<a href="/url">link title</a>

There are shortcuts for setting the class and id attributes using [] accessors:

>>> print(h.div["#header bg-white"](h.span["text-xl font-medium"]("hello")))
<div id="header" class="bg-white"><span class="text-xl font-medium">hello</span></div>

Text content, attribute names and attribute values are escaped automatically. To include unescaped content, use the raw element. Only use this with trusted input:

>>> print(h.div(foo='"bar"')("2 > 1"))
<div foo="&quot;bar&quot;">2 &gt; 1</div>
>>> print(h.script(h.raw('if (2 > 1) console.log("math still works");')))
<script>if (2 > 1) console.log("math still works");</script>

To use tag or attribute names that conflict with python keywords, append an underscore. Underscores within attribute names are converted to hyphens ("-", except for a single underscore, which is passed through unchanged).

>>> print(h.del_("deleted text"))
<del>deleted text</del>
>>> print(h.label(for_="fieldname")("text"))
<label for="fieldname">text</label>
>>> print(h.span(data_foo="bar"))
<span data-foo="bar"></span>
>>> print(h.span(_="something"))
<span _="something"></span>

Attributes that have no value can be set by passing the value True:

>>> print(h.input(type="text", disabled=True))
<input type="text" disabled />

Use minihtml.tostring to convert a tag to a string and add a doctype:

>>> from minihtml import tostring
>>> html = h.html(h.body("my website"))
>>> tostring(html)
'<!doctype html>\n<html><body>my website</body></html>\n'

License

minihtml is licensed under the MIT license. See the included file LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minihtml-0.1.4.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

minihtml-0.1.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file minihtml-0.1.4.tar.gz.

File metadata

  • Download URL: minihtml-0.1.4.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for minihtml-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3cd314f13680000d10efc7e4d2307dc24a51187d8c2e7e7297e252f165ac69a4
MD5 9b7affe7b4d03255c7489dac934728ba
BLAKE2b-256 44e4ec4f0c3dfddf0470174cb86b21eda46a003c2d401bdf674d26851c4bef0b

See more details on using hashes here.

File details

Details for the file minihtml-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: minihtml-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for minihtml-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3e7d9e6f927321b35e46f61675b3a494c10bfa3833c2bb7e583ef52cc59f5ef3
MD5 6026e0f676084c44e88b176c8ad425d1
BLAKE2b-256 22794ba6d019e955a2b629d7091e5ee62da14402535522a0c9f015b690259c69

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page