Skip to main content

generate html with python 3

Project description

domonic: generate html with python 3!
domonic
Generate html with python 3!

from domonic 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>

install

    python3 -m pip install domonic

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>

rendering

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

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

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"} )

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

common errors

TODO - catch these and raise a friendly error that tells you what to fix

IndexError: list index out of range - You most likely didn't put a underscore on an attribute

SyntaxError: invalid syntax - You most likely are missing a comma somewhere between params

run tests

python3 test_domonic.py

more

several undocumented features. take a look at the code.

disclaimer

exerimental/learning project

There's a complete more supported library I found already doing similar called 'dominate' . So if you want to do something like this, use that.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

domonic-0.0.1.tar.gz (26.6 kB view details)

Uploaded Source

File details

Details for the file domonic-0.0.1.tar.gz.

File metadata

  • Download URL: domonic-0.0.1.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for domonic-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f132821dbd7d763832eeb49ce6c547ce1aa8695a6fe124612b59b401556254ac
MD5 e1d1b72d63f034123cc408fd270d7ddd
BLAKE2b-256 add082580d7e011e5c9a40b8b90671cbd3d3460f760bee902260f46e96e7611e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page