Skip to main content

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]

Project details

Release history Release notifications

This version


Download files

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

Files for OOHTML, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size (1.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page