Registry / web-framework / dominate

dominate

JSON →
library2.9.1pypypi✓ verified 25d ago

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 dominate
INSTALL
IMPORT
SIG · DOMINATE
D
dominate
web-frameworkpythonv2.9.1
Install
1.5s avg
Import
232ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.9.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.244s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.220s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

document
from dominate.document import document
Used to create a full HTML document structure.
tags
from dominate import tags
Commonly imported as 'tags' to access HTML elements like tags.html, tags.body, tags.h1, etc.
html
from dominate.tags import html, body, h1
Direct import of specific tags for convenience.
text
from dominate.util import text
Useful for inserting raw text that should not be further escaped or parsed as HTML.

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.

from dominate.document import document from dominate.tags import html, head, body, h1, p, a, div, br from dominate.util import text doc = document(title='My Awesome Page') with doc.head: a(href='https://example.com', _class='link-style', data_info='example') << 'Example Link' with doc.body: h1('Hello, Dominate!') p('This is a paragraph created with Python.') with div(id='container'): p('Another paragraph inside a div.') p('Line one.') br() p('Line two with explicit break.') p('Raw text with newline:\n') text('This text should appear on a new line after the explicit break.') # Render the document to an HTML string html_output = doc.render() print(html_output) # Example of rendering with pretty printing off # print(doc.render(pretty=False))
Debug
Known issues
gotchaNewline characters (`\n`) in Python strings are not automatically converted to HTML `<br>` tags when inserted into Dominate elements. You must explicitly add `<br/>` tags or use `dominate.util.text` if you intend for newlines to be preserved visually in the rendered HTML, or `p` tags for paragraphs.
fix
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.
affects: All versions
gotchaWhen adding attributes to HTML tags, Python keywords (e.g., `class`, `for`) must be escaped with an underscore, like `_class='my-class'` or `_for='my-label'` to avoid syntax errors.
fix
Prefix Python reserved keywords used as HTML attributes with an underscore (e.g., `_class`, `_for`).
affects: All versions
breakingVersion 2.9.1 introduced support for `dominate` to work in async contexts. While this is an enhancement, improper use of `dominate` elements or context managers within `async` functions without awaiting where necessary could lead to unexpected behavior or runtime errors in `asyncio` applications.
fix
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).
affects: >=2.9.1
gotchaWhen reusing tag instances (e.g., creating a tag and then adding content to it multiple times in different contexts), be aware of Dominate's context management. If a tag instance is created outside a `with` block and then used inside multiple `with` blocks, its content might be duplicated or not appear where expected.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dominate'
The 'dominate' library is not installed in your Python environment, or there is a local Python file named 'dominate.py' that is shadowing the actual library.
fix
Run `pip install dominate` to install the library. If already installed, rename any local script files named `dominate.py` to avoid conflicts.
dominate not rendering newlines as <br> or escaping raw HTML
By default, `dominate` treats '\n' as a literal character in text content and escapes raw HTML strings to prevent cross-site scripting (XSS) vulnerabilities, rather than converting them into HTML `<br>` tags or embedding them unescaped.
fix
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>'))`.
TypeError: unsupported operand type(s) for +: 'p' and 'document'
You are attempting to concatenate or use mathematical operators like '+' directly with `dominate` tag or document objects, which is not supported for modifying the document structure in this manner, especially when trying to prepend elements to a document.
fix
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.
AttributeError: module 'dominate.tags' has no attribute 'input'
You are trying to access an HTML tag (like 'input') that either does not exist, is misspelled, or is not directly exposed as an attribute of the `dominate.tags` module with that specific casing or name.
fix
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).
Upgrade
Version history
2.9.1latest on PyPI · released Dec 24, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
18
Resources
dominate — pip install dominate · libregistry