Domonic: HTML

rendering

render takes 2 parameters, some domonic and an optional output file.

page = div(span('Hello World'))
render(page, 'index.html')

templating

from domonic.html import *

  output = render(
      html(
          head(
              style(),
              script(),
          ),
          body(
              div("hello world"),
              a("this is a link", _href="http://www.somesite.com", _style="font-size:10px;"),
              ol(''.join([f'{li()}' for thing in range(5)])),
              h1("test", _class="test"),
          )
      )
  )
<html><head><style></style><script></script></head><body><div>hello world</div><a href="http://www.somesite.com" style="font-size:10px;">this is a link</a><ol><li></li><li></li><li></li><li></li><li></li></ol><h1 class="test">test</h1></body></html>

Take a look in tests/test_html.py at the bootstrap5 alpha examples. All tests passed on several templates.

usage

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

attributes

prepend attributes with an underscore ( avoids clashing with python keywords )

test = label(_class='classname', _for="someinput")
print(test)
<label class="classname" for="someinput"></label>

lists

just do list comprehension and join it to strip the square brackets

ul(''.join([f'{li()}' for thing in range(5)])),
<ul><li></li><li></li><li></li><li></li></ul>

data-tags

python doesn’t allow hyphens in parameter names. so use variable keyword argument syntax for custom data-tags

div("test", **{"_data-test":"test"} )

DONT FORGET TO PREPEND THE UNDERSCORE.

fugly

use your own methods to prettify. the example uses a library that leverages beautifulsoup. i.e.

output = render(html(body(h1('Hello, World!'))))
from html5print import HTMLBeautifier
print(HTMLBeautifier.beautify(output, 4))

createElement

to create your own elements use the DOM API

from domonic.dom import *
from domonic.html import *

site = html()
el = document.createElement('myelement')
site.appendChild(el)
print(site)

For more info about the DOM API navigate to that section…

Magic methods

Multiply

You can quickly clone nodes with a multiplier which will return a list…

from domonic.html import *
mydivs = div()*100

but you will have to render them yourself by interating and calling string…

print(''.join([str(c) for c in mydivs]))

Divide

A divisor also creates more but will instead call render and give a list of strings…

from domonic.html import *
print(div()/100)

but this means they are now rendered and can’t be edited.

Although you could convert them back by calling parser then domonify. i.e.

mylist = li()/10
myobj = domonic.domonify(domonic.parse(mylist))
print(myobj)

OR

If other is anything, it is returned. Otherwise it returns self

from domonic.html import *
print(div() | False)
print(div() | True)

Another way is to use ternary i.e.

mything = div() if True else span(class-'warning')

In place add/minus

You can add to or remove from the children of a Node with the in-place operators…

myorderedlist = ol()
myorderedlist += str(li() / 10)
print(myorderedlist)

This also works for text nodes but be aware they will be irreversibly flattened if you render…

a1 = button()
a1 += "hi"
a1 += "how"
a1 += "are"
a1 += "you"
print(a1)
a1 -= "hi"
print(a1)

Pass a dictionary to the right shift operator to add/update an attribute… (don’t forget underscore or it will error)

a1 = img()
a1 >> {'_src': "http://www.someurl.com"}
print(a1)

Access an elements children as if it were a list…

mylist = ul(li(1), li(2), li(3))
print(mylist[1])

unpack children…

mylist = ul(li(), li(), li())
print(*mylist)
a1, b1, c1 = ul(li(1), li(2), li(3))
print(a1)
a1, b1, c1, d1, e1 = button() * 5
print(a1, b1, c1, d1, e1)

domonic.html

Generate HTML using python 3

exception domonic.html.TemplateError(error, message='Templating error: ')[source]
class domonic.html.a(*args, **kwargs)
class domonic.html.abbr(*args, **kwargs)
class domonic.html.address(*args, **kwargs)
class domonic.html.area(*args, **kwargs)
class domonic.html.article(*args, **kwargs)
class domonic.html.aside(*args, **kwargs)
class domonic.html.audio(*args, **kwargs)
class domonic.html.b(*args, **kwargs)
class domonic.html.base(*args, **kwargs)
class domonic.html.bdi(*args, **kwargs)
class domonic.html.bdo(*args, **kwargs)
class domonic.html.blockquote(*args, **kwargs)
class domonic.html.body(*args, **kwargs)
class domonic.html.br(*args, **kwargs)
class domonic.html.button(*args, **kwargs)
class domonic.html.canvas(*args, **kwargs)
class domonic.html.caption(*args, **kwargs)
class domonic.html.cite(*args, **kwargs)
class domonic.html.closed_tag(*args, **kwargs)[source]
class domonic.html.code(*args, **kwargs)
class domonic.html.col(*args, **kwargs)
class domonic.html.colgroup(*args, **kwargs)
class domonic.html.command(*args, **kwargs)
class domonic.html.comment(content='')[source]
Parameters:content (str) – Message to be rendered inside the comment tag
Returns:“<!– {self.content} –>
Return type:str
class domonic.html.datalist(*args, **kwargs)
class domonic.html.dd(*args, **kwargs)
class domonic.html.details(*args, **kwargs)
class domonic.html.dfn(*args, **kwargs)
class domonic.html.div(*args, **kwargs)
class domonic.html.dl(*args, **kwargs)
class domonic.html.doctype[source]
Returns:<!DOCTYPE html>
Return type:str
class domonic.html.dt(*args, **kwargs)
class domonic.html.em(*args, **kwargs)
class domonic.html.embed(*args, **kwargs)
class domonic.html.fieldset(*args, **kwargs)
class domonic.html.figcaption(*args, **kwargs)
class domonic.html.figure(*args, **kwargs)
class domonic.html.font(*args, **kwargs)
class domonic.html.footer(*args, **kwargs)
class domonic.html.form(*args, **kwargs)
class domonic.html.h1(*args, **kwargs)
class domonic.html.h2(*args, **kwargs)
class domonic.html.h3(*args, **kwargs)
class domonic.html.h4(*args, **kwargs)
class domonic.html.h5(*args, **kwargs)
class domonic.html.h6(*args, **kwargs)
class domonic.html.head(*args, **kwargs)
class domonic.html.header(*args, **kwargs)
class domonic.html.hgroup(*args, **kwargs)
class domonic.html.hr(*args, **kwargs)
class domonic.html.html(*args, **kwargs)
class domonic.html.i(*args, **kwargs)
class domonic.html.iframe(*args, **kwargs)
class domonic.html.img(*args, **kwargs)
class domonic.html.input(*args, **kwargs)
class domonic.html.ins(*args, **kwargs)
class domonic.html.kbd(*args, **kwargs)
class domonic.html.keygen(*args, **kwargs)
class domonic.html.label(*args, **kwargs)
class domonic.html.legend(*args, **kwargs)
class domonic.html.li(*args, **kwargs)
class domonic.html.link(*args, **kwargs)
domonic.html.main

alias of domonic.html.command

class domonic.html.mark(*args, **kwargs)
class domonic.html.menu(*args, **kwargs)
class domonic.html.meta(*args, **kwargs)
class domonic.html.meter(*args, **kwargs)
class domonic.html.nav(*args, **kwargs)
class domonic.html.noscript(*args, **kwargs)
class domonic.html.ol(*args, **kwargs)
class domonic.html.optgroup(*args, **kwargs)
class domonic.html.option(*args, **kwargs)
class domonic.html.output(*args, **kwargs)
class domonic.html.p(*args, **kwargs)
class domonic.html.param(*args, **kwargs)
class domonic.html.portal(*args, **kwargs)
class domonic.html.pre(*args, **kwargs)
class domonic.html.progress(*args, **kwargs)
class domonic.html.q(*args, **kwargs)
domonic.html.render(inp, outp='')[source]

Renders the input to string or to a file.

Parameters:
  • inp (obj) – A domonic tag. For example div()
  • outp (str) – An optional output filename
Returns:

A HTML rendered string

Return type:

str

class domonic.html.rp(*args, **kwargs)
class domonic.html.rt(*args, **kwargs)
class domonic.html.ruby(*args, **kwargs)
class domonic.html.s(*args, **kwargs)
class domonic.html.samp(*args, **kwargs)
class domonic.html.script(*args, **kwargs)
class domonic.html.section(*args, **kwargs)
class domonic.html.select(*args, **kwargs)
class domonic.html.small(*args, **kwargs)
class domonic.html.source(*args, **kwargs)
class domonic.html.span(*args, **kwargs)
class domonic.html.strong(*args, **kwargs)
class domonic.html.style(*args, **kwargs)
class domonic.html.sub(*args, **kwargs)
class domonic.html.submit(*args, **kwargs)
class domonic.html.summary(*args, **kwargs)
class domonic.html.sup(*args, **kwargs)
class domonic.html.table(*args, **kwargs)
class domonic.html.tag(*args, **kwargs)[source]

The class from which all html tags extend

class domonic.html.tbody(*args, **kwargs)
class domonic.html.td(*args, **kwargs)
class domonic.html.textarea(*args, **kwargs)
class domonic.html.tfoot(*args, **kwargs)
class domonic.html.th(*args, **kwargs)
class domonic.html.thead(*args, **kwargs)
class domonic.html.title(*args, **kwargs)
class domonic.html.tr(*args, **kwargs)
class domonic.html.track(*args, **kwargs)
class domonic.html.u(*args, **kwargs)
class domonic.html.ul(*args, **kwargs)
class domonic.html.var(*args, **kwargs)
class domonic.html.video(*args, **kwargs)
class domonic.html.wbr(*args, **kwargs)