https://github.com/jgrugru/htmlfactory
A simple way to produce HTML with Python.
https://github.com/jgrugru/htmlfactory
create html markup-language python xml
Last synced: 6 months ago
JSON representation
A simple way to produce HTML with Python.
- Host: GitHub
- URL: https://github.com/jgrugru/htmlfactory
- Owner: jgrugru
- License: mit
- Created: 2021-02-12T20:55:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-03-11T21:59:45.000Z (over 4 years ago)
- Last Synced: 2025-11-27T16:41:22.394Z (7 months ago)
- Topics: create, html, markup-language, python, xml
- Language: Python
- Homepage:
- Size: 137 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# htmlfactory [](https://travis-ci.com/jgrugru/htmlfactory)
A simple way to produce HTML with Python.
Source code can be found on [github](https://github.com/jgrugru/htmlfactory).
```Python
pip install htmlfactory
```
**htmlfactory** simplifies the process of making HTML through Python.
#### Examples:
###### basic div example
```Python
TagFactory("div.my-class")
```
```html
```
To add content between the divs, we can pass a string or *TagFactory* objects.
```Python
# pass a string
TagFactory("div.my-class", 'I am inside the div.')
```
```html
I am inside the div.
```
```Python
# pass a TagFactory object
TagFactory("div.my-class", TagFactory("div", "child tag"))
```
```html
child tag
```
###### children div example
pass a list of *TagFactory* objects
```Python
TagFactory("div.parent-div", [
TagFactory("div.first-child-div", (
TagFactory("div.second-child-div", "It's party time.")))])
```
```html
It's party time.
```
>Note:
>Children tags can be passed through a list, tuple, or singular *TagFactory* object.
###### printing *TagFactory* objects
To output a TagFactory object, use print.
```Python
print(TagFactory('div', TagFactory('form')))
```
```html
```
Use the function *pretty_str()* for an indented output.
```Python
print(TagFactory('div', TagFactory('form')).pretty_str())
```
```html
```
If you would like an HTML, body, and head tag to be included, pass *add_html_tags=True*.
```Python
print(TagFactory('div', TagFactory('form')).pretty_str(add_html_tags=True))
```
```html
```
###### multiple classes example
You can add as many classes as you want to your tag object:
```Python
TagFactory("div.class1.class2.class3.class4.class5", 'I have a lot of classes.')
```
```html
I have a lot of classes.
```
###### adding attributes example
You can add attributes to your tab object by using keyword arguments:
```Python
TagFactory("form", 'I have an action & method attribute.', action="/action_page.php", method="get")
```
```html
I have an action and method attribute.
```
>Note:
>'for' is a keyword so it cannot be used as a keyword argument. Instead use 'four'.
>Example: ```TagFactory("div.my-class", "inside the div", four="my-form")```
>Dashes (-) also cause a similar problem. For all html attributes that require a dash,
> omit the dash. The dash will be added upon creation of the object.
```Python
# with an omitted dash
TagFactory("div", role="application", ariadescribedby="info")
```
```html
```
###### adding tags without closing brackets example
You can create tags without closing brackets, which may be useful if wanting to add an img or link:
```Python
test_tag = SingletonTag("img", border="0", alt="TestTag",
src="logo_w3s.gif", width="100",
height="100")
```
```html
```
The SingletonTag class is inherited to the TagFactory class, so SingletonTags can be treated like TagFactory objects and added as children html elements to other TagFactory objects.
```Python
a_tag = TagFactory("a", SingletonTag("img", src="logo_w3s.gif"),
href="www.google.com")
```