• Stars
    star
    1,706
  • Rank 27,360 (Top 0.6 %)
  • Language
    Python
  • License
    GNU Lesser Genera...
  • Created over 15 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dominate 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 eliminate the need to learn another template language, and to take advantage of the more powerful features of Python.

Dominate

Dominate 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.

Python version Build status Coverage status

Python:

import dominate
from dominate.tags import *

doc = dominate.document(title='Dominate your HTML')

with doc.head:
    link(rel='stylesheet', href='style.css')
    script(type='text/javascript', src='script.js')

with doc:
    with div(id='header').add(ol()):
        for i in ['home', 'about', 'contact']:
            li(a(i.title(), href='/%s.html' % i))

    with div():
        attr(cls='body')
        p('Lorem ipsum..')

print(doc)

Output:

<!DOCTYPE html>
<html>
  <head>
    <title>Dominate your HTML</title>
    <link href="style.css" rel="stylesheet">
    <script src="script.js" type="text/javascript"></script>
  </head>
  <body>
    <div id="header">
      <ol>
        <li>
          <a href="/home.html">Home</a>
        </li>
        <li>
          <a href="/about.html">About</a>
        </li>
        <li>
          <a href="/contact.html">Contact</a>
        </li>
      </ol>
    </div>
    <div class="body">
      <p>Lorem ipsum..</p>
    </div>
  </body>
</html>

Installation

The recommended way to install dominate is with pip:

pip install dominate

PyPI version PyPI downloads

Developed By

Git repository located at github.com/Knio/dominate

Examples

All examples assume you have imported the appropriate tags or entire tag set:

from dominate.tags import *

Hello, World!

The most basic feature of dominate exposes a class for each HTML element, where the constructor accepts child elements, text, or keyword attributes. dominate nodes return their HTML representation from the __str__, __unicode__, and render() methods.

print(html(body(h1('Hello, World!'))))
<html>
    <body>
        <h1>Hello, World!</h1>
    </body>
</html>

Attributes

Dominate can also use keyword arguments to append attributes onto your tags. Most of the attributes are a direct copy from the HTML spec with a few variations.

For attributes class and for which conflict with Python's reserved keywords, you can use the following aliases:

class for
_class _for
cls fr
className htmlFor
class_name html_for
test = label(cls='classname anothername', fr='someinput')
print(test)
<label class="classname anothername" for="someinput"></label>

Use data_* for custom HTML5 data attributes.

test = div(data_employee='101011')
print(test)
<div data-employee="101011"></div>

You can also modify the attributes of tags through a dictionary-like interface:

header = div()
header['id'] = 'header'
print(header)
<div id="header"></div>

Complex Structures

Through the use of the += operator and the .add() method you can easily create more advanced structures.

Create a simple list:

list = ul()
for item in range(4):
    list += li('Item #', item)
print(list)
<ul>
    <li>Item #0</li>
    <li>Item #1</li>
    <li>Item #2</li>
    <li>Item #3</li>
</ul>

dominate supports iterables to help streamline your code:

print(ul(li(a(name, href=link), __pretty=False) for name, link in menu_items))
<ul>
    <li><a href="/home/">Home</a></li>
    <li><a href="/about/">About</a></li>
    <li><a href="/downloads/">Downloads</a></li>
    <li><a href="/links/">Links</a></li>
</ul>

A simple document tree:

_html = html()
_body = _html.add(body())
header  = _body.add(div(id='header'))
content = _body.add(div(id='content'))
footer  = _body.add(div(id='footer'))
print(_html)
<html>
    <body>
        <div id="header"></div>
        <div id="content"></div>
        <div id="footer"></div>
    </body>
</html>

For clean code, the .add() method returns children in tuples. The above example can be cleaned up and expanded like this:

_html = html()
_head, _body = _html.add(head(title('Simple Document Tree')), body())
names = ['header', 'content', 'footer']
header, content, footer = _body.add([div(id=name) for name in names])
print(_html)
<html>
    <head>
       <title>Simple Document Tree</title>
    </head>
    <body>
        <div id="header"></div>
        <div id="content"></div>
        <div id="footer"></div>
    </body>
</html>

You can modify the attributes of tags through a dictionary-like interface:

header = div()
header['id'] = 'header'
print(header)
<div id="header"></div>

Or the children of a tag though an array-line interface:

header = div('Test')
header[0] = 'Hello World'
print(header)
<div>Hello World</div>

Comments can be created using objects too!

print(comment('BEGIN HEADER'))
<!--BEGIN HEADER-->
print(comment(p('Upgrade to newer IE!'), condition='lt IE9'))
<!--[if lt IE9]>
  <p>Upgrade to newer IE!</p>
<![endif]-->

Rendering

By default, render() tries to make all output human readable, with one HTML element per line and two spaces of indentation.

This behavior can be controlled by the __pretty (default: True except for certain element types like pre) attribute when creating an element, and by the pretty (default: True), indent (default: ) and xhtml (default: False) arguments to render(). Rendering options propagate to all descendant nodes.

a = div(span('Hello World'))
print(a.render())
<div>
  <span>Hello World</span>
</div>
print(a.render(pretty=False))
<div><span>Hello World</span></div>
print(a.render(indent='\t'))
<div>
	<span>Hello World</span>
</div>
a = div(span('Hello World'), __pretty=False)
print(a.render())
<div><span>Hello World</span></div>
d = div()
with d:
    hr()
    p("Test")
    br()
print(d.render())
print(d.render(xhtml=True))
<div>
  <hr>
  <p>Test</p><br>
</div>
<div>
  <hr />
  <p>Test</p><br />
</div>

Context Managers

You can also add child elements using Python's with statement:

h = ul()
with h:
    li('One')
    li('Two')
    li('Three')

print(h)
<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

You can use this along with the other mechanisms of adding children elements, including nesting with statements, and it works as expected:

h = html()
with h.add(body()).add(div(id='content')):
    h1('Hello World!')
    p('Lorem ipsum ...')
    with table().add(tbody()):
        l = tr()
        l += td('One')
        l.add(td('Two'))
        with l:
            td('Three')

print(h)
<html>
    <body>
        <div id="content">
            <h1>Hello World!</h1>
            <p>Lorem ipsum ...</p>
            <table>
                <tbody>
                    <tr>
                        <td>One</td>
                        <td>Two</td>
                        <td>Three</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

When the context is closed, any nodes that were not already added to something get added to the current context.

Attributes can be added to the current context with the attr function:

d = div()
with d:
    attr(id='header')

 print(d)
<div id="header"></div>

And text nodes can be added with the dominate.util.text function:

from dominate.util import text
para = p(__pretty=False)
with para:
    text('Have a look at our ')
    a('other products', href='/products')

print(para)
<p>Have a look at our <a href="/products">other products</a></p>

Decorators

Dominate is great for creating reusable widgets for parts of your page. Consider this example:

def greeting(name):
    with div() as d:
        p('Hello, %s' % name)
    return d

print(greeting('Bob'))
<div>
    <p>Hello, Bob</p>
</div>

You can see the following pattern being repeated here:

def widget(parameters):
    with tag() as t:
        ...
    return t

This boilerplate can be avoided by using tags (objects and instances) as decorators

@div
def greeting(name):
    p('Hello %s' % name)
print(greeting('Bob'))
<div>
    <p>Hello Bob</p>
</div>

The decorated function will return a new instance of the tag used to decorate it, and execute in a with context which will collect all the nodes created inside it.

You can also use instances of tags as decorators, if you need to add attributes or other data to the root node of the widget. Each call to the decorated function will return a copy of the node used to decorate it.

@div(h2('Welcome'), cls='greeting')
def greeting(name):
    p('Hello %s' % name)

print(greeting('Bob'))
<div class="greeting">
    <h2>Welcome</h2>
    <p>Hello Bob</p>
</div>

Creating Documents

Since creating the common structure of an HTML document everytime would be excessively tedious dominate provides a class to create and manage them for you: document.

When you create a new document, the basic HTML tag structure is created for you.

d = document()
print(d)
<!DOCTYPE html>
<html>
    <head>
       <title>Dominate</title>
    </head>
    <body></body>
</html>

The document class accepts title, doctype, and request keyword arguments. The default values for these arguments are Dominate, <!DOCTYPE html>, and None respectively.

The document class also provides helpers to allow you to access the title, head, and body nodes directly.

d = document()
>>> d.head
<dominate.tags.head: 0 attributes, 1 children>
>>> d.body
<dominate.tags.body: 0 attributes, 0 children>
>>> d.title
u'Dominate'

The document class also provides helpers to allow you to directly add nodes to the body tag.

d = document()
d += h1('Hello, World!')
d += p('This is a paragraph.')
print(d)
<!DOCTYPE html>
<html>
    <head>
       <title>Dominate</title>
    </head>
    <body>
        <h1>Hello, World!</h1>
        <p>This is a paragraph.</p>
    </body>
</html>

Embedding HTML

If you need to embed a node of pre-formed HTML coming from a library such as markdown or the like, you can avoid escaped HTML by using the raw method from the dominate.util package:

from dominate.util import raw
...
td(raw('<a href="example.html">Example</a>'))

Without the raw call, this code would render escaped HTML with lt, etc.

SVG

The dominate.svg module contains SVG tags similar to how dominate.tags contains HTML tags. SVG elements will automatically convert _ to - for dashed elements. For example:

from dominate.svg import *
print(circle(stroke_width=5))
<circle stroke-width="5"></circle>

More Repositories

1

pynmea2

Python library for parsing the NMEA 0183 protocol (GPS)
Python
630
star
2

carhack

Automotive data aquisition, logging, diagnostics, and visualization project. Focusing on CAN-bus and the Nissan 370Z
JavaScript
72
star
3

myfitbit

Fitbit data export - Because your data belongs to you!
Python
34
star
4

sshsec

SSH server parameter tester
Python
21
star
5

scenerename

script to guess and batch rename media files using tvdb
Python
18
star
6

structpack

A Python library for serializing and deserializing object trees to JSON-compatable values (dicts, lists, strings, ints, floats, bools)
Python
11
star
7

komoot-export

Export all Komoot tours as GPX
Python
10
star
8

dominate.js

JavaScript library focused on quick and simple creation and manipulation of DOM elements
JavaScript
10
star
9

treecat

Displays a tree view of files and their contents
Python
9
star
10

httpproxy

A HTTP proxy server written in C++
C++
8
star
11

SmallptGPU

C
7
star
12

sync

a poor replacement of rsync, and a very efficient file copy utility
Python
6
star
13

everdb-native

C implementation of everdb - Fast and efficient on-disk data structures and embedded databases
C++
6
star
14

everdb

Fast and efficient on-disk data structures and embedded databases
5
star
15

prettytraceback

Pretty tracebacks for Python everywhere
Python
3
star
16

plok

A JavasSript library for rendering interactive and real-time graphs of time-series data
JavaScript
3
star
17

threespace

A python library for interfacing with the YEI 3-Space Sensor
Python
2
star
18

kniopass

A simple CLI password manager
Python
2
star
19

everdb-go

everdb is an embedded database that implements fast efficient on-disk data structures
Go
2
star
20

tumblr-export

Export tumblr posts and likes, including photos and videos
Python
2
star
21

nethealth

Python
2
star
22

Domino

Go
1
star
23

py35win64ext

An example C extension module for 64bit Python 3.5 on Windows
C
1
star
24

ultigpx

Automatically exported from code.google.com/p/ultigpx
Java
1
star
25

tag2itunes

writes ID3 tags to an iTunes database
Python
1
star
26

knio

linux config files
Python
1
star
27

tex2png

A python library to convert TeX strings to PNG images for embedding into webpages
Python
1
star
28

eRacer

3D racing game developed for games programming class at university of calgary
C++
1
star