Release 0.4.8
Total Page:16
File Type:pdf, Size:1020Kb
domonic Release 0.6.0 Sep 30, 2021 Contents: 1 HTML TEMPLATING 3 2 install 5 3 usage 7 4 The User Guide 9 4.1 Domonic: HTML.............................................9 4.2 Domonic: DOM............................................. 19 4.3 Domonic: Javascript........................................... 35 4.4 Domonic: events............................................. 54 4.5 Domonic: sitemap............................................ 60 4.6 Domonic: dQuery............................................ 62 4.7 Domonic: d3............................................... 73 4.8 Domonic: JSON............................................. 74 4.9 Domonic: constants........................................... 76 4.10 Domonic: terminal............................................ 123 4.11 Domonic: cmd.............................................. 130 4.12 Domonic: tween............................................. 136 4.13 Domonic: geom............................................. 137 4.14 Domonic: x3d.............................................. 137 4.15 Domonic: CDN............................................. 158 4.16 Domonic: decorators........................................... 164 4.17 Domonic: templates and components.................................. 166 4.18 Domonic: utils.............................................. 170 4.19 Domonic: servers............................................. 174 4.20 Domonic: autodocs............................................ 182 5 CLI 263 6 Join-In 265 7 EXAMPLE PROJECT 267 8 Disclaimer 269 Python Module Index 271 i Index 273 ii domonic, Release 0.6.0 • genindex • modindex • search Domonic Not only a Python library for generating HTML Domonic contains several main packages: (but by no means are any of them complete) • html : Generate html with python 3 • dom : DOM API in python 3 • javascript : js API in python 3 • terminal || cmd : call terminal commands with python3 (see at the end) • JSON : utils for loading / decorating / transformin • SVG : Generate svg using python (untested • aframe || x3d tags : auto generate 3d worlds with aframe. (see examples folder • dQuery - Utils for querying domonic. (alt + 0 for the º symbol) • geom - vec2, vec3 with _dunders_ as well as shape classes Contents: 1 domonic, Release 0.6.0 2 Contents: CHAPTER 1 HTML TEMPLATING from domonic.html import * mydom= 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"), ) ) render(mydom) <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> To pretty print a domonic dom you can now use a f-string. Which also adds the doctype .. i.e. print(f"{mydom}") 3 domonic, Release 0.6.0 4 Chapter 1. HTML TEMPLATING CHAPTER 2 install python3-m pip install domonic or if you had it before upgrade: python3-m pip install domonic--upgrade 5 domonic, Release 0.6.0 6 Chapter 2. install CHAPTER 3 usage print(html(body(h1('Hello, World!')))) <html><body><h1>Hello, World!</h1></body></html> 7 domonic, Release 0.6.0 8 Chapter 3. usage CHAPTER 4 The User Guide Here you can find some instructions for getting the most out of Domonic. 4.1 Domonic: HTML 4.1.1 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') 4.1.2 templating from domonic.html import * output= render( html( head( style(), script(), ), body( div("hello world"), (continues on next page) 9 domonic, Release 0.6.0 (continued from previous page) 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. 4.1.3 usage print(html(body(h1('Hello, World!')))) <html><body><h1>Hello, World!</h1></body></html> 4.1.4 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> 4.1.5 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> 4.1.6 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. 10 Chapter 4. The User Guide domonic, Release 0.6.0 4.1.7 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 ="" """), 4.1.8 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; } } """), 4.1.9 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) 4.1. Domonic: HTML 11 domonic, Release 0.6.0 For more info about the DOM API navigate to that section. 4.1.10 Decorators You can use decorators to wrap elements around function results 4.1.11 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. 12 Chapter 4. The User Guide domonic, Release 0.6.0 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) 4.1.12 f-strings To pretty print a domonic dom you can use a f-string. print(f"{mydom}") <!DOCTYPE html> <html> <body> <h1>Hello, World!</h1> </body> </html> which basically calls the format dunder on the tag. (if look at the code) This is useful as it means use different ways to get output from domonic. print(f"{mydom}") print(f"{mydom!s}") print(f"{mydom!r}") print(f"{mydom!a}") print(str(mydom)) (continues on next page) 4.1. Domonic: HTML 13 domonic, Release 0.6.0 (continued from previous page) print(mydom.__format__('')) If the built in formatter is not up to your needs. You can also use other libraries that leverage beautifulsoup i.e. output= render(html(body(h1('Hello, World!')))) from html5print import HTMLBeautifier print(HTMLBeautifier.beautify(output,4)) For outputting pyml to a string there is a method in production called __pyml__() which may become repr. (but i’d been saving repr for logging) You can also use this vscode plugin on .pyml and it does a nice job. (inpsiration for the ‘dentage’ method) https://marketplace.visualstudio.com/items?itemName=mgesbert.indent-nested-dictionary 4.1.13 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 4.1.14 parsing You can hook domonic to the super fast html5 c++ parser.