Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scrussell24/hype-html
A minimal python dsl for generating html
https://github.com/scrussell24/hype-html
Last synced: 4 months ago
JSON representation
A minimal python dsl for generating html
- Host: GitHub
- URL: https://github.com/scrussell24/hype-html
- Owner: scrussell24
- License: mit
- Created: 2020-11-14T20:09:53.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-10T04:32:51.000Z (11 months ago)
- Last Synced: 2024-04-11T20:55:36.011Z (10 months ago)
- Language: Python
- Size: 122 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-html - scrussell24/hype-html
- awesome-python-html - scrussell24/hype-html
README
# 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)
#
#Hello World!
```Elements can also be rendered by calling their render method.
```
doc.render() == str(doc)
```
### Inner HTMLArguments 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)
#
#Hello World
#This is a paragraph
Just a string
#Another paragraph
#
```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)
#
#Hello World
#This is a paragraph
Just a string
#Another paragraph
#
```### Attributes
Attributes are passed as keyword arguments to the element's constructor.
```
span = Span('span', width='50px')print(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
```
Any remaining underscores will be converted to hyphens.```
span = Span('span', custom_attrbiute='custom')print(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
```### 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)#
```
To create a custom self closing tag, subtype the SelfClosingElement class.```
class CustomTag(SelfClosingElement):
tag = 'custom-tag'tag = CustomTag()
print(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))#
#Header
#
```### 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 aynciofrom hype.asyncio import *
div = Div(H1('Header'))
result = asyncio.run(div.render)
print(result)#
#Header
#```