A Python library for creating and manipulating HTML documents using an elegant DOM API
—
Complete HTML document creation and management with automatic structure generation, metadata handling, and content organization.
Creates a complete HTML document with proper DOCTYPE, head, and body structure. Automatically sets up common sections (header, main, footer) and provides direct access to document components.
class document(html):
def __init__(self, title='Dominate', doctype='<!DOCTYPE html>', *a, **kw):
"""
Create a new HTML document.
Parameters:
- title (str): Document title, defaults to 'Dominate'
- doctype (str): DOCTYPE declaration, defaults to '<!DOCTYPE html>'
- *a: Additional child elements
- **kw: Additional attributes
"""from dominate import document
from dominate.tags import *
# Create document with custom title
doc = document(title='My Website')
# Access document components
print(doc.title) # 'My Website'
print(doc.head) # <head> element
print(doc.body) # <body> element
# Document automatically includes header, main, footer containers
print(doc.header) # container for header content
print(doc.main) # container for main content
print(doc.footer) # container for footer contentGet and set the document title with automatic HTML element management.
@property
def title(self) -> str:
"""Get the document title text."""
@title.setter
def title(self, value: str | html_tag):
"""
Set the document title.
Parameters:
- value: String for simple title, or html_tag for complex title element
"""doc = document()
# Get title
current_title = doc.title
# Set simple title
doc.title = 'New Title'
# Set complex title with custom element
doc.title = title('Complex Title', lang='en')Add content directly to the document body or to specific sections.
def add(self, *args):
"""
Add elements to the document body (main section by default).
Parameters:
- *args: Elements to add to the document
Returns:
- Added elements (single element if one arg, tuple if multiple)
"""doc = document(title='My Site')
# Add content to main section (default)
doc.add(h1('Welcome'))
doc.add(p('This is the main content.'))
# Add to specific sections
with doc.header:
nav(ul(li(a('Home', href='/'))))
with doc.footer:
p('© 2023 My Company')
# Multiple elements at once
heading, content = doc.add(h2('Section'), div(cls='content'))Render the complete document with DOCTYPE and proper HTML structure.
def _render(self, sb, *args, **kwargs):
"""
Render the complete document including DOCTYPE.
Parameters:
- sb: String buffer for output
- *args: Rendering arguments
- **kwargs: Rendering options (pretty, indent, xhtml)
Returns:
- String buffer with rendered document
"""doc = document(title='Example')
doc.add(h1('Hello World'))
# Render complete document
html_output = str(doc)
# Output:
# <!DOCTYPE html>
# <html>
# <head>
# <title>Example</title>
# </head>
# <body>
# <div> <!-- header container -->
# </div>
# <div> <!-- main container -->
# <h1>Hello World</h1>
# </div>
# <div> <!-- footer container -->
# </div>
# </body>
# </html>
# Custom rendering options
compact_html = doc.render(pretty=False)
custom_indent = doc.render(indent='\t')
xhtml_output = doc.render(xhtml=True)Get string representation of the document for debugging.
def __repr__(self):
"""
Return string representation showing document title.
Returns:
- str: Representation in format '<dominate.document "title">'
"""doc = document(title='My Website')
print(repr(doc)) # <dominate.document "My Website">import dominate
from dominate.tags import *
# Create document
doc = dominate.document(title='Complete Example')
# Add metadata
with doc.head:
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1')
link(rel='stylesheet', href='style.css')
# Add header
with doc.header:
nav(
ul(
li(a('Home', href='/')),
li(a('About', href='/about')),
li(a('Services', href='/services')),
li(a('Contact', href='/contact'))
),
cls='main-nav'
)
# Add main content
with doc.main:
section(
h1('Welcome to Our Website'),
p('This is the main content area with important information.'),
div(
h2('Our Services'),
ul(
li('Web Development'),
li('Design Services'),
li('Consulting')
)
),
cls='hero-section'
)
# Add footer
with doc.footer:
p('© 2023 Our Company. All rights reserved.')
p('Contact us at info@example.com')
# Output the complete document
print(doc)Install with Tessl CLI
npx tessl i tessl/pypi-dominate