Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Object Oriented HTML Construction Toolkit for Python

Project Description

# OOHTML Object Oriented HTML Construction Toolkit for Python

Simple Usage

you can construct HTML in two ways, first with feeding the Constructor:

>>> from OOHTML import *
>>> C = Constructor()
>>> C.feed(o('html'),                  #open HTML tag
             o('head'),                #open Head
             c('head'),                #close Head
                 r('Hello World!'),    #raw content
                 s('area'),            #singleton (self closing element)

Or using class methods, the above example can be rewriten as:

>>> from OOHTML import *
>>> C = Constructor()
>>>'div').raw('Hello World!').single('area').close('div')
>>> C.close('body').close('html')

to render the constructor as html, you can use render() method:

>>> C.render()

Each of opening, singleton or raw elements can have an id (not the HTML ID of the element) so they can be accessible:

>>> R = r('Hello World', 'message') #a raw element with id 'message'
>>> O = o('div', {}, 'menu')        #opening element with id 'menu'
>>> S = s('img', {}, 'bg')          #singleton with id 'bg'
>>> C.feed(R)
>>> C.getElementById('message')
>>> C.removeElementById('message')

You can insert a Constructor object inside another Constructor:

>>> sampleConstructor = Constructor()
>>> C.addAfterId('sampleID', sampleConstructor)

There are numerous methods for accessing elements and changing its data:

>>> C.getElementById('message').value          #Raw elements use .value to store their data
>>> C.getElementById('sample').attributes      #Opening and Singleton elements have .attribute
>>> C.getElementById('sample').tag             #Opening, Closing & Singleton elements have .tag
>>> C.getElementById('sample').id              #All elements have .id

Opening and Singleton Elements can have attributes:

>>> div = o('div', {'class':['message', 'floater']}, 'contents')
>>> C.feed(div)
>>> div.addAtributes({'id': ['sample']})

You can use this attributes to select elements:

>>> C.getElementsByAttributes({'class': ['test']})    #This returns a generator

To show you an example:

>>> from OOHTML import *
>>> C = Constructor()

>>> C.feed(o('html'),
           o('script', {'type': ['text/javascript'], 'src':['script.js']}),
           o('style', {'type': ['text/css'], 'src':['style.css']}),
           o('div', {'class':['message']}),
             r('Hello World!!!', 'message'),
             s('img', {'alt': ['Hello'], 'src':['img.jpeg']}),
           o('div', {'class':['message', 'floater']}, 'contents'), c('div'),

>>> C.getElementById('message').value = "HTML CONSTRUCTOR!!!"
>>> C.addAfterId('contents', Constructor().open('div').raw('OOHTML').single('br').close('div'))
>>> print(C.render())

The above outputs:

        <script type = "text/javascript" src = "script.js">
        <style type = "text/css" src = "style.css">
        <div class = "message">
            HTML CONSTRUCTOR!!!
            <img src = "img.jpeg" alt = "Hello"/>
        <div class = "message floater">

Explore source code for more info.


Download package from here: or simply install with pip install OOHTML

Project Info

Github project page: PyPi: Mail me at: persian.writer [at]

Release History

Release History

This version
History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date (1.9 kB) Copy SHA256 Checksum SHA256 Source Apr 11, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting