Skip to main content

hype

A minimal python dsl for generating html.

Install

Install via pip

pip install hype-html

Alternatively, 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 their render method.

doc.render() == 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>

Append to an elements' list of inner html elements.

body = Body(
    H1('Hello World'),
    P('This is a paragraph'),
    'Just a string'
)

body.append(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>

Also add attributes using a method's attrs method add keyword arguments.

span = Span('span')
span.attrs(_id='my-span', test='test')

print(span)

# <span id="my-span" test="test">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>

Async Elements

If your elements content's use coroutines you can use the async elements in hype.asyncio.

The API is nearly identical with two main differences. Of course, the render method is a coroutine. Because of they do not render when calling the str function on them You must explicitly call their render method instead.

import ayncio

from hype.asyncio import *


div = Div(H1('Header'))

result = asyncio.run(div.render)
print(result)

# <div>
#    <h1>Header</h1>
# </div>

Metadata

Release files for hype-html 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hype-html 2.1.2
File Size Uploaded
hype_html-2.1.2.tar.gz 18.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hype-html 2.1.2
File Interpreter ABI Platform
hype_html-2.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 39.1 kB

Release files / hype_html-2.1.2.tar.gz

Download URL hype_html-2.1.2.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6f522a11ce880e32638529717a66354e79936663d6b9cda46a61b7c56322bcb4
BLAKE2b-256 checksum
How to use checksums
6408715111af3e41f317bcfdb5ff354301e729aca25377b395eb6b4dcf702b5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-92-generic

Release files / hype_html-2.1.2-py3-none-any.whl

Download URL hype_html-2.1.2-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa1286fee494804f6b95a3d8ec3c69154d34e84dec081852d30db822536afa79
BLAKE2b-256 checksum
How to use checksums
e695c156919bad306a4fc645e2151e8091f0394179b0a1cb58e493306a734356
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-92-generic

Release history Release notifications | RSS feed

This release

2.1.2 This release

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page