Skip to content

Repository files navigation

minihtml - Simple HTML Generation

PyPI - Version

minihtml is a library to generate HTML documents from Python. It aims to provide an API that allows you to define the structure of an HTML document in a succinct and natural way.

By building up nested HTML elements using context managers, you can combine HTML generation with control flow statements in a way that is easy to read and does not obscure the structure of the resulting HTML document.

Installation

Install the minihtml package from PyPI:

pip install minihtml

or

uv add minihtml

Example

A basic "hello, world" example:

>>>fromminihtml.tagsimporthtml, head, title, body, div, p, a, img, ul, li>>>links= [("Home", "/"), ("About Me", "/about"), ("Projects", "/projects")]
>>>withhtml(lang="en") aselem:
... withhead:
... title("hello, world!")
... withbody, div["#content main"]:
... p("Welcome to ", a(href="https://example.com/")("my website"))
... img(src="hello.png", alt="hello")
... withul:
... fortitle, urlinlinks:
... li(a(href=url)(title))
...
<...>>>>print(elem)
<htmllang="en"><head><title>hello, world!</title></head><body><divid="content"class="main"><p>Welcometo<ahref="https://example.com/">mywebsite</a></p><imgsrc="hello.png"alt="hello"><ul><li><ahref="/">Home</a></li><li><ahref="/about">AboutMe</a></li><li><ahref="/projects">Projects</a></li></ul></div></body></html>

Links

License

Minihtml is released under the MIT license. See LICENSE for more information.

About

Simple HTML Generation for Python

Resources

Stars

13 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages