Dominate is a Python library (current version 2.9.1) for creating and manipulating HTML documents using an elegant DOM API. It allows developers to write HTML pages concisely in pure Python, eliminating the need for a separate template language. The library is actively maintained with a regular release cadence.
pip install dominateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating a basic HTML document, adding elements to the head and body, nesting elements using `with` statements, and adding attributes including custom data attributes. It also shows how to explicitly add line breaks and handle raw text, highlighting a common 'gotcha' with newlines.
Use `dominate.tags.br()` for explicit line breaks or wrap text in appropriate block-level elements like `p()`. For raw text that should not be escaped and contains newlines, consider inserting it with `dominate.util.text()` into a `<pre>` tag or similar.
Prefix Python reserved keywords used as HTML attributes with an underscore (e.g., `_class`, `_for`).
When using `dominate` within asynchronous code, ensure that any operations that might block the event loop are handled appropriately. If a `dominate` component is designed to be an `async` context manager or interact with `async` resources, ensure `await` is used correctly with `__aenter__` and `__aexit__` (though `dominate` itself does not inherently require `await` for its core DOM manipulation, new integrations might).
When using `with` statements, each `with` block implicitly adds created elements to the current context. If you intend to build up a single element incrementally, add content directly to its `.add()` method or `+=` operator, or ensure the instance is correctly managed within a single `with` context for its children.
Run `pip install dominate` to install the library. If already installed, rename any local script files named `dominate.py` to avoid conflicts.
For line breaks, explicitly use `from dominate.tags import br; br()` or `doc.add(br())`. To insert pre-formatted HTML without escaping, use `from dominate.util import raw; some_tag(raw('<a href="example.html">Example</a>'))`.Instead of direct concatenation, use the `add()` method of a parent tag to append child elements (e.g., `parent_tag.add(child_tag)`). For more complex structural changes, especially prepending to a document, you might need to reconstruct the document or manage elements within a container.
Refer to the `dominate` library's documentation or source code for the correct names and casing of HTML tags. Ensure you are importing all necessary tags, for example, by using `from dominate.tags import *` or explicitly importing `from dominate.tags import input_` (note the underscore for `input` as it's a Python keyword).
No dependency data recorded yet.