A Python library for creating and manipulating HTML documents using an elegant DOM API
—
All HTML5 elements as Python classes with full attribute support, context manager integration, and proper rendering. Each element supports the complete DOM API with Python-friendly attribute naming.
Foundation class for all HTML elements providing common functionality and attribute handling.
class html_tag(dom_tag):
def __init__(self, *args, **kwargs):
"""
Create an HTML element.
Parameters:
- *args: Child elements or text content
- **kwargs: HTML attributes (class_, fr, data_* become class, for, data-*)
"""Core document structure and metadata elements.
def html(*args, **kwargs): ... # Root HTML element
def head(*args, **kwargs): ... # Document metadata container
def body(*args, **kwargs): ... # Document body content
def title(*args, **kwargs): ... # Document title
def meta(*args, **kwargs): ... # Metadata (self-closing)
def link(*args, **kwargs): ... # External resource links (self-closing)
def base(*args, **kwargs): ... # Base URL (self-closing)
def style(*args, **kwargs): ... # CSS styles (no pretty printing)
def script(*args, **kwargs): ... # JavaScript (no pretty printing)
def noscript(*args, **kwargs): ... # No-script fallbackfrom dominate.tags import *
# Complete document structure
page = html(
head(
title('My Page'),
meta(charset='utf-8'),
meta(name='viewport', content='width=device-width, initial-scale=1'),
link(rel='stylesheet', href='styles.css'),
style('body { margin: 0; }'),
script(src='script.js'),
script('console.log("inline script");')
),
body(
h1('Welcome'),
p('Content here')
)
)Semantic document structure and content organization.
def main(*args, **kwargs): ... # Main content area
def section(*args, **kwargs): ... # Generic sections
def nav(*args, **kwargs): ... # Navigation links
def article(*args, **kwargs): ... # Self-contained content
def aside(*args, **kwargs): ... # Sidebar content
def header(*args, **kwargs): ... # Introductory content
def footer(*args, **kwargs): ... # Footer content
def address(*args, **kwargs): ... # Contact information
# Headings
def h1(*args, **kwargs): ... # Primary heading
def h2(*args, **kwargs): ... # Secondary heading
def h3(*args, **kwargs): ... # Tertiary heading
def h4(*args, **kwargs): ... # Fourth-level heading
def h5(*args, **kwargs): ... # Fifth-level heading
def h6(*args, **kwargs): ... # Sixth-level heading
def hgroup(*args, **kwargs): ... # Heading group# Semantic page structure
page_content = main(
header(
h1('Article Title'),
p('Published on January 1, 2023')
),
section(
h2('Introduction'),
p('This is the introduction section.')
),
article(
h2('Main Article'),
p('Article content goes here.'),
aside('Related sidebar information')
),
footer(
address('Contact: author@example.com')
)
)Elements for organizing and grouping content.
def div(*args, **kwargs): ... # Generic container
def p(*args, **kwargs): ... # Paragraph
def hr(*args, **kwargs): ... # Thematic break (self-closing)
def pre(*args, **kwargs): ... # Preformatted text (no pretty printing)
def blockquote(*args, **kwargs): ... # Quoted content
def figure(*args, **kwargs): ... # Self-contained content
def figcaption(*args, **kwargs): ... # Figure caption
# Lists
def ol(*args, **kwargs): ... # Ordered list
def ul(*args, **kwargs): ... # Unordered list
def li(*args, **kwargs): ... # List item
def dl(*args, **kwargs): ... # Description list
def dt(*args, **kwargs): ... # Description term
def dd(*args, **kwargs): ... # Description definition# Content grouping
content = div(
p('This is a paragraph with some text.'),
hr(), # Horizontal rule
blockquote(
p('This is a quoted passage.'),
cite='https://example.com'
),
figure(
pre('def example():\n return "code"'),
figcaption('Code example')
),
ul(
li('First item'),
li('Second item'),
li('Third item')
),
dl(
dt('Term 1'), dd('Definition 1'),
dt('Term 2'), dd('Definition 2')
)
)Inline text formatting and semantic markup.
def a(*args, **kwargs): ... # Links/anchors
def em(*args, **kwargs): ... # Emphasis
def strong(*args, **kwargs): ... # Strong importance
def small(*args, **kwargs): ... # Fine print
def s(*args, **kwargs): ... # Strikethrough
def cite(*args, **kwargs): ... # Citations
def q(*args, **kwargs): ... # Inline quotes
def dfn(*args, **kwargs): ... # Definitions
def abbr(*args, **kwargs): ... # Abbreviations
def time_(*args, **kwargs): ... # Time/dates (also available as _time)
def code(*args, **kwargs): ... # Code
def var(*args, **kwargs): ... # Variables
def samp(*args, **kwargs): ... # Sample output
def kbd(*args, **kwargs): ... # Keyboard input
def sub(*args, **kwargs): ... # Subscript
def sup(*args, **kwargs): ... # Superscript
def i(*args, **kwargs): ... # Italic (inline)
def b(*args, **kwargs): ... # Bold
def u(*args, **kwargs): ... # Underline
def mark(*args, **kwargs): ... # Highlighted text
def span(*args, **kwargs): ... # Generic inline
def br(*args, **kwargs): ... # Line break (self-closing, inline)
def wbr(*args, **kwargs): ... # Word break opportunity (self-closing, inline)
# Ruby annotations
def ruby(*args, **kwargs): ... # Ruby annotation
def rt(*args, **kwargs): ... # Ruby text
def rp(*args, **kwargs): ... # Ruby parentheses
# Bidirectional text
def bdi(*args, **kwargs): ... # Bidirectional isolation
def bdo(*args, **kwargs): ... # Bidirectional override# Text semantics
text_content = p(
'This is ', em('emphasized'), ' text with ', strong('strong'), ' importance. ',
'Here is a ', a('link', href='https://example.com'), ' and some ',
code('inline code'), '. The price is ', small('$19.99'), '.',
br(),
'Published on ', time_('2023-01-01', datetime='2023-01-01'), '.',
br(),
'Chemical formula: H', sub('2'), 'O and E=mc', sup('2'), '.'
)Elements for indicating content changes.
def ins(*args, **kwargs): ... # Inserted content
def del_(*args, **kwargs): ... # Deleted content (also available as _del)# Edit tracking
edited_text = p(
'The price is ',
del_('$29.99', title='Old price'),
' ',
ins('$19.99', title='New price')
)Media and external content embedding.
def img(*args, **kwargs): ... # Images (self-closing)
def iframe(*args, **kwargs): ... # Nested browsing context
def embed(*args, **kwargs): ... # External application (self-closing)
def object_(*args, **kwargs): ... # External resource (also available as _object)
def param(*args, **kwargs): ... # Object parameters (self-closing)
def video(*args, **kwargs): ... # Video content
def audio(*args, **kwargs): ... # Audio content
def source(*args, **kwargs): ... # Media sources (self-closing)
def track(*args, **kwargs): ... # Text tracks (self-closing)
def canvas(*args, **kwargs): ... # Drawing canvas
def map_(*args, **kwargs): ... # Image map (also available as _map)
def area(*args, **kwargs): ... # Image map area (self-closing)# Embedded content
media_content = div(
img(src='photo.jpg', alt='A beautiful photo', width='400'),
video(
source(src='video.mp4', type='video/mp4'),
source(src='video.webm', type='video/webm'),
track(src='captions.vtt', kind='captions', srclang='en'),
'Your browser does not support video.',
controls=True
),
audio(
source(src='audio.mp3', type='audio/mpeg'),
'Your browser does not support audio.',
controls=True
),
canvas(id='myCanvas', width='300', height='200')
)Tabular data presentation.
def table(*args, **kwargs): ... # Table container
def caption(*args, **kwargs): ... # Table caption
def colgroup(*args, **kwargs): ... # Column group
def col(*args, **kwargs): ... # Column (self-closing)
def thead(*args, **kwargs): ... # Table header
def tbody(*args, **kwargs): ... # Table body
def tfoot(*args, **kwargs): ... # Table footer
def tr(*args, **kwargs): ... # Table row
def th(*args, **kwargs): ... # Header cell
def td(*args, **kwargs): ... # Data cell# Data table
data_table = table(
caption('Sales Report'),
colgroup(
col(span='2', style='background-color: #f0f0f0'),
col(style='background-color: #e0e0e0')
),
thead(
tr(
th('Product', scope='col'),
th('Quantity', scope='col'),
th('Price', scope='col')
)
),
tbody(
tr(
td('Widget A'),
td('10'),
td('$100')
),
tr(
td('Widget B'),
td('5'),
td('$50')
)
),
tfoot(
tr(
th('Total', scope='row', colspan='2'),
td('$150')
)
)
)Interactive forms and input controls.
def form(*args, **kwargs): ... # Form container
def fieldset(*args, **kwargs): ... # Form control group
def legend(*args, **kwargs): ... # Fieldset caption
def label(*args, **kwargs): ... # Input label
def input_(*args, **kwargs): ... # Form input (self-closing, also available as _input)
def button(*args, **kwargs): ... # Button
def select(*args, **kwargs): ... # Select dropdown
def datalist(*args, **kwargs): ... # Input suggestions
def optgroup(*args, **kwargs): ... # Option group
def option(*args, **kwargs): ... # Select option
def textarea(*args, **kwargs): ... # Multi-line text input
def output(*args, **kwargs): ... # Calculation result
def progress(*args, **kwargs): ... # Progress indicator
def meter(*args, **kwargs): ... # Scalar measurement
def keygen(*args, **kwargs): ... # Key generator (self-closing)# Contact form
contact_form = form(
fieldset(
legend('Personal Information'),
label(
'Name: ',
input_(type='text', name='name', required=True)
),
label(
'Email: ',
input_(type='email', name='email', required=True)
)
),
fieldset(
legend('Message'),
label(
'Subject: ',
select(
option('General Inquiry'),
option('Support Request'),
option('Feedback'),
name='subject'
)
),
label(
'Message: ',
textarea(name='message', rows='5', cols='40', required=True)
)
),
button('Send Message', type='submit'),
method='post',
action='/contact'
)User interaction and disclosure widgets.
def details(*args, **kwargs): ... # Disclosure widget
def summary(*args, **kwargs): ... # Details summary
def menu(*args, **kwargs): ... # Command menu
def command(*args, **kwargs): ... # User command (self-closing)# Interactive disclosure
disclosure = details(
summary('Click to expand'),
p('This content is hidden by default.'),
p('It becomes visible when the summary is clicked.')
)Legacy elements and special-purpose elements.
def font(*args, **kwargs): ... # Font styling (legacy)
def comment(*args, **kwargs): ... # HTML commentsThe comment element supports conditional comments for Internet Explorer:
# Regular comment
regular_comment = comment('This is a regular HTML comment')
# IE conditional comment
ie_comment = comment(
p('Please upgrade your browser'),
condition='lt IE9'
)
# Downlevel conditional comment
downlevel_comment = comment(
p('You are using a downlevel browser'),
condition='false',
downlevel='revealed'
)All HTML elements support Python-friendly attribute naming with automatic conversion:
# Attribute shortcuts and conversions
element = div(
cls='my-class', # becomes class="my-class"
class_name='other-class', # becomes class="other-class"
fr='input-id', # becomes for="input-id"
html_for='input-id', # becomes for="input-id"
data_value='123', # becomes data-value="123"
aria_label='Description' # becomes aria-label="Description"
)
# Boolean attributes
checkbox = input_(type='checkbox', checked=True) # becomes checked="checked"
disabled_button = button('Click', disabled=True) # becomes disabled="disabled"Install with Tessl CLI
npx tessl i tessl/pypi-dominate