Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chingc/pyhtml
Write and format HTML manually with Python!
https://github.com/chingc/pyhtml
html html-generator
Last synced: 3 days ago
JSON representation
Write and format HTML manually with Python!
- Host: GitHub
- URL: https://github.com/chingc/pyhtml
- Owner: chingc
- License: mit
- Created: 2012-11-07T21:30:47.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2018-11-03T05:18:52.000Z (about 6 years ago)
- Last Synced: 2023-03-23T01:40:54.822Z (over 1 year ago)
- Topics: html, html-generator
- Language: Python
- Homepage:
- Size: 98.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyhtml
[![CircleCI](https://circleci.com/gh/chingc/pyhtml.svg?style=shield)](https://circleci.com/gh/chingc/workflows/pyhtml) [![codecov](https://codecov.io/gh/chingc/pyhtml/branch/master/graph/badge.svg)](https://codecov.io/gh/chingc/pyhtml) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
For those who love Python and, for reasons unknown, still like to write and format HTML manually.
## Installation
1. Add `pyhtml.py` to your project
1. `import pyhtml`## Reference
### new
This is the first thing to call and will return a new instance of PyHTML.
```python
>>> pyhtml.new()
```It can take the following arguments:
- `doctype: str` - doctype declaration (default: "")
- valid doctypes: html5, html4.01s, html4.01t, html4.01f, xhtml1.1, xhtml1.0s, xhtml1.0t, xhtml1.0f
- `spaces: int` - number of spaces used for indentation (default: 4)For html4.01 and xhtml1.0: s, t, and f stands for strict, transitional, and frameset, respectively.
### attr
Strings and tuples are stringified into HTML attribute form.
```python
>>> pyhtml.attr(("src", "simpsons.webm"), "autoplay", ("width", 800), ("height", 600))
'src="simpsons.webm" autoplay width="800" height="600"'
```Strings are treated as boolean attributes and 2-tuples are treated as value attributes. Tuples can take the form (str, str) or (str, int). Multiple comma separated strings and tuples can be specified.
### append
- `string: str` -- add arbitrary text to the HTML
### indent
Add indentation. Indentation is handled automatically by default. This is useful during manual spacing.
### newline
Add a newline. Newlines are handled automatically by default. This is useful during manual spacing or when additional newlines are desired.
### vwrap
Add a void element. These are elements that do not have a closing tag.
- `elem: str` -- an HTML void element
- `attrs: str` -- element attributes (default: "")```python
>>> html = pyhtml.new()
>>> html.vwrap("img", pyhtml.attr(("src", "kwikemart.png"), ("width", 358), ("height", 278)))
>>> print(html)
```### wrap
Add an element. The closing tag will be inserted automatically.
- `elem: str` -- an HTML element
- `attrs: str` -- element attributes (default: "")```python
>>> html = pyhtml.new()
>>> with html.wrap("div", pyhtml.attr(("class", "big"))):
... html.append("Embiggened!")
...
>>> print(html)
Embiggened!
```### manual_spacing
Disable automatic indentation and newlines. Statements within the block will not have automatic indentation or newlines.
```python
>>> html = pyhtml.new()
>>> with html.manual_spacing():
... with html.wrap("em"):
... html.append("Itchy")
... html.append(" & ")
... with html.wrap("em"):
... html.append("Scratchy")
...
>>> print(html)
Itchy & Scratchy
```## Examples
```python
>>> import pyhtml
>>> html = pyhtml.new()
>>> with html.wrap("html"):
... with html.wrap("head"):
... with html.wrap("title"):
... html.append("Dr. Nick")
... with html.wrap("body"):
... with html.wrap("p"):
... html.append("Hi, everybody!")
...
>>> print(html)
Dr. Nick
Hi, everybody!
```
```python
# Disable auto-spacing when you want to have
# a wrapped element on a single line.>>> import pyhtml
>>> html = pyhtml.new()
>>> with html.wrap("html"):
... with html.wrap("head"):
... with html.manual_spacing():
... html.indent()
... with html.wrap("title"):
... html.append("Ned Flanders")
... html.newline()
... with html.wrap("body"):
... with html.manual_spacing():
... html.indent()
... with html.wrap("p"):
... html.append("Hi diddly ho!")
... html.newline()
...
>>> print(html)
Ned Flanders
Hi diddly ho!
```
```python
# Tip: Context managers can be nested.>>> import pyhtml
>>> html = pyhtml.new()
>>> family = ["homer", "marge", "bart", "lisa", "maggie"]
>>> with html.wrap("ul"), html.manual_spacing():
... for member in family:
... html.indent()
... with html.wrap("li"):
... html.append(member)
... html.newline()
...
>>> print(html)
- homer
- marge
- bart
- lisa
- maggie
```