Skip to main content

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


Download files

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

Source Distribution

hype-html-0.0.6.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

hype_html-0.0.6-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file hype-html-0.0.6.tar.gz.

File metadata

  • Download URL: hype-html-0.0.6.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

Hashes for hype-html-0.0.6.tar.gz
Algorithm Hash digest
SHA256 f06c67befd572a2f05b793aff906c841daff461c32e137ad700e50300d66f3be
MD5 eae53526ca9603a68c1e89a313808951
BLAKE2b-256 7271223b9fdeb93c1a7f2458a4ee1560f6310d48229e95765a508640193b70e6

See more details on using hashes here.

File details

Details for the file hype_html-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: hype_html-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 10.6 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

Hashes for hype_html-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 dfd72f64c84190b4d73fa232066a2f71297eb7b5d69937d3123ea791e358abe8
MD5 7c7e676e55e4ad2208f308273e502ce9
BLAKE2b-256 5703c6cee64bfb8d47339a7790f2009d8dd3410606e8ff15b42d55412cec479c

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