A minimal python dsl for generating html.
Project description
hype
A minimal python dsl for generating html.
Install
Install via pip
pip install hype-html
Alteratively, in the spirit of removing dependencies, you could simply copy/paste the hype/element.py file into your project.
Usage
Hype exposes classes for each html tag.
from hype import *
doc = Doc(Html('Hello World!'))
print(doc)
# <!DOCTYPE html>
# <div>Hello World!</div>
Elements can also be rendered by calling.
doc() == str(doc)
Inner HTML
Arguments passed in the element constructor are rendered using the str function and indented (if the element only has one argument it will not be indented.)
body = Body(
H1('Hello World'),
P('This is a paragraph'),
'Just a string',
P('Another paragraph')
)
print(body)
# <body>
# <h1>Hello World</h1>
# <p>This is a paragraph</p>Just a string
# <p>Another paragraph</p>
# </body>
Attributes
Attributes are passed as keyword arguments to the element's constructor.
span = Span('span', width='50px')
print(span)
# <span width="50px">span</span>
Since some built-in, and possibly custom, attributes conflict with python keywords, keywords will automatically have all leading underscores stripped.
span = Span('span', _id='my-span', width='50px')
print(span)
# <span id="my-span" width="50px">span</span>
Any remaining underscores will be converted to hyphens.
span = Span('span', custom_attrbiute='custom')
print(span)
# <span custom-attribute="custom">span</span>
Custom Elements
If you to create a custom tag, just subtype the Element class and add a tag.
class CustomTag(Element):
tag = 'custom-tag'
tag = CustomTag()
print(tag)
# <custom-tag></custom-tag>
To create a custom self closing tag, subtype the SelfClosingElement class.
class CustomTag(SelfClosingElement):
tag = 'custom-tag'
tag = CustomTag()
print(tag)
# <custom-tag/>
Indents
The indent to be used can be passed as a keyword arg to the Doc constructor.
doc = Doc(content, indent=Indent.TAB)
It can also be passed as a keyword arg when calling and element.
div = Div(H1('Header'))
print(div(indent=Indent.FOUR_SPACES))
# <div>
# <h1>Header</h1>
# </div>
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
Built Distribution
File details
Details for the file hype-html-0.0.5.tar.gz
.
File metadata
- Download URL: hype-html-0.0.5.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56a646a42751bce4288fbad982b4becb6f146a6d419361eca2324cb27a4d4abe |
|
MD5 | 3656a81795c45bcf11d241926c03cd34 |
|
BLAKE2b-256 | e9348af7eb27f36d8e11a7e4d6ed1c4e56a24e585cb0a44dce1b2a10928d191b |
File details
Details for the file hype_html-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: hype_html-0.0.5-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 977fd5d38eb064e5559d0d2d6542c8b9780fb89033d7802df271e7177ad964de |
|
MD5 | 4435af9a6fd9f6094d93086ddf18d22c |
|
BLAKE2b-256 | 2ecfa7084e26b4c3a152cd691b102cece524b9b31bea66dc6c2c317b29767fa8 |