A Python library for creating and manipulating HTML documents using an elegant DOM API
npx @tessl/cli install tessl/pypi-dominate@2.9.0Dominate is a Python library for creating and manipulating HTML documents using an elegant DOM API. It allows you to write HTML pages in pure Python very concisely, which eliminates the need to learn another template language, and lets you take advantage of the more powerful features of Python.
pip install dominateimport dominate
from dominate.tags import *For specific modules:
from dominate import document
from dominate.tags import div, p, a, html_tag
from dominate.svg import svg, circle, rect
from dominate.util import text, raw, escape
from dominate.dom_tag import dom_tag, attr, get_currentimport dominate
from dominate.tags import *
# Create a complete HTML document
doc = dominate.document(title='My Website')
# Add content using context managers
with doc.head:
link(rel='stylesheet', href='style.css')
script(type='text/javascript', src='script.js')
with doc:
with div(id='header'):
h1('Welcome to My Site')
nav(
ul(
li(a('Home', href='/')),
li(a('About', href='/about')),
li(a('Contact', href='/contact'))
)
)
with div(cls='content'):
p('This is the main content area.')
p('Dominate makes HTML generation easy!')
print(doc)Dominate follows a hierarchical DOM-like structure:
dom_tag: Base class for all HTML elements providing core functionalityhtml_tag: Extends dom_tag with HTML-specific features and DOM Level 1 Core APIdocument: Complete HTML document container with head, body, and metadata managementwith statementThe library supports both imperative element creation and declarative context-based composition, making it suitable for both simple HTML generation and complex template systems.
Complete HTML document management with automatic structure generation, metadata handling, and section organization (header, main, footer).
class document(html):
def __init__(self, title='Dominate', doctype='<!DOCTYPE html>', *args, **kwargs): ...
@property
def title(self) -> str: ...
@title.setter
def title(self, value: str): ...
def add(self, *args): ...All HTML5 elements as Python classes with full attribute support, context manager integration, and proper rendering. Includes semantic elements, forms, tables, media, and interactive elements.
class html_tag(dom_tag):
def __init__(self, *args, **kwargs): ...
# Semantic elements
def html(*args, **kwargs): ...
def head(*args, **kwargs): ...
def body(*args, **kwargs): ...
def div(*args, **kwargs): ...
def p(*args, **kwargs): ...
def a(*args, **kwargs): ...
# Form elements
def form(*args, **kwargs): ...
def input_(*args, **kwargs): ...
def button(*args, **kwargs): ...
def select(*args, **kwargs): ...
# Table elements
def table(*args, **kwargs): ...
def tr(*args, **kwargs): ...
def td(*args, **kwargs): ...
def th(*args, **kwargs): ...SVG elements for vector graphics creation with automatic attribute name conversion (underscore to dash) and support for shapes, paths, gradients, filters, and animations.
class svg_tag(html_tag):
@staticmethod
def clean_attribute(attribute): ...
def svg(*args, **kwargs): ...
def circle(*args, **kwargs): ...
def rect(*args, **kwargs): ...
def path(*args, **kwargs): ...
def g(*args, **kwargs): ...
def text(*args, **kwargs): ...
def linearGradient(*args, **kwargs): ...
def filter(*args, **kwargs): ...Core DOM tree manipulation with element creation, attribute management, content addition/removal, searching, and rendering with customizable formatting.
class dom_tag:
def __init__(self, *args, **kwargs): ...
def add(self, *args): ...
def remove(self, obj): ...
def clear(self): ...
def get(self, tag=None, **kwargs): ...
def set_attribute(self, key, value): ...
def render(self, indent=' ', pretty=True, xhtml=False): ...
def __enter__(self): ...
def __exit__(self, type, value, traceback): ...
def attr(*args, **kwargs): ...
def get_current(default=None): ...Text processing utilities for HTML escaping, URL encoding, raw HTML insertion, file inclusion, and specialized container elements for advanced layouts.
def escape(data, quote=True): ...
def unescape(data): ...
def url_escape(data): ...
def url_unescape(data): ...
def raw(s): ...
def include(f): ...
class container(dom_tag): ...
class text(dom_tag):
def __init__(self, _text, escape=True): ...
class lazy(dom_tag):
def __init__(self, func, *args, **kwargs): ...# Base types
class dom_tag:
is_single: bool # Self-closing tag
is_pretty: bool # Pretty print content
is_inline: bool # Inline rendering
attributes: dict
children: list
parent: dom_tag | None
class html_tag(dom_tag): ...
class document(html_tag):
doctype: str
head: html_tag
body: html_tag
title_node: html_tag
header: container
main: container
footer: container
# Utility types
class container(dom_tag): ...
class text(dom_tag):
escape: bool
text: str
class lazy(dom_tag):
func: callable
args: tuple
kwargs: dict