Domonic: HTML

rendering

you can cast str() on any element to render it.

el_string = str(div())
print(el_string)

there’s also a render method that takes 2 parameters, some pyml and an optional output file.

from domonic.html import *
    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.

script tags

load from a source…

script(_src="/docs/5.0/dist/js/bootstrap.bundle.min.js", _integrity="sha384-1234", _crossorigin="anonymous"),

or do inline js…

    script("""
let itbe = ""
"""),

style tags

load from a source…

link(_href="/docs/5.0/dist/css/bootstrap.min.css", _rel="stylesheet", __integrity="sha384-12345", __crossorigin="anonymous"),

or do inline css…

style("""
.bd-placeholder-img {
    font-size: 1.125rem;
    text-anchor: middle;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
    font-size: 3.5rem;
}
}
"""),

Create Elements

to create your own custom elements you can use create_element

from domonic.html import *
create_element('custom_el', div('some content'), _id="test")

or you could 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…

Decorators

You can use decorators to wrap elements around function results

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)

Fugly

for now 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))

Some primitive formatting is coming shortly

You can also use this vscode plugin on .pyml and it does a nice job.

https://marketplace.visualstudio.com/items?itemName=mgesbert.indent-nested-dictionary

loading .pyml templates

‘loads’ imports a pyml file and turns it into a program

this example loads a template and passing params for rendering

from domonic import loads
from domonic.html import *

# create some vars. you will see these referenced in the template file
brand = "MyBrand"
links = ['one', 'two', 'three']

# load a template and pass it some data
webpage = domonic.loads('templates/webpage.com.pyml', links=links, brand=brand)

render(webpage, 'webpage.html')

# ‘load’ is different to ‘loads’, it takes html strings and converts to a program

from domonic.dQuery import º

webpage = domonic.load('<html><head></head><body id="test"></body></html>')
º(webpage)
º('#test').append(div("Hello World"))
render(webpage, 'webpage2.html')
  • warning loads also is very basic and can only convert simple html as the parser is still in development

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.applet(*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.basefont(*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.center(*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
domonic.html.create_element(name='custom_tag', *args, **kwargs)[source]

If you must, a method for creating custom tags tag name needs to be set due to custom tags with hyphens can’t be classnames. i.e. hypenated tags <some-custom-tag></some-custom-tag>

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)[source]
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.isindex(*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)
class domonic.html.listing(*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.picture(*args, **kwargs)
class domonic.html.plaintext(*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.strike(*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.template(*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)
class domonic.html.xmp(*args, **kwargs)