Skip to main content

A beautiful html builder library.

Project description

HtmlBuilder

HtmlBuilder is a python library that allows you to render HTML files by writing python code. And to make use of python features, clean syntax, and object-oriented design to their full potential.

codecov python version badge

Why should you care about this library?

When rendering HTML programmatically, there are other options available (template engines and other rendering libraries). Still, these are often limited in what they can do, or it's necessary to learn a new level of abstraction before being productive. HtmlBuilder tries to improve on this by following the next few ideas:

  • Minimal learning curve: Users should need no more than Python and HTML knowledge to be productive using this tool.
  • Real python code: The final code looks and behaves as you would expect from other python code.
  • Easily testable: Users can introspect and unit test the HTML object structure before rendering the HTML string.

Installation

run pip install htmlbuilder

Use examples:

A simple example

# import necessary tags and attributes
from htmlBuilder.tags import *
from htmlBuilder.attributes import Class, Style as InlineStyle


# html tags are represented by classes 
html = Html([],
    # any tag can receive another tag as constructor parameter
    Head([],
        Title([], "A beautiful site")
    ),
    Body([Class('btn btn-success'), InlineStyle(background_color='red', bottom='35px')],
        Hr(),
        Div([],
            Div()
        )
    )
)
# no closing tags are required

# call the render() method to return tag instances as html text
print(html.render(pretty=True))

Output

    <html>
      <head>
        <title>
          A beautiful site
        </title>
      </head>
      <body class='btn btn-success' style='background-color: red; bottom: 35px'>
        <hr/>
        <div>
          <div></div>
        </div>
      </body>
    </html>

A not so simple example

from htmlBuilder.attributes import Class
from htmlBuilder.tags import Html, Head, Title, Body, Nav, Div, Footer, Ul, Li

# declare data
users = [
    {
        "name": "Jose",
        "movies": ['A beautiful mind', 'Red'],
        "favorite-number": 42,
    },
    {
        "name": "Jaime",
        "movies": ['The breakfast club', 'Fight club'],
        "favorite-number": 7,
    },
    {
        "name": "Jhon",
        "movies": ['The room', 'Yes man'],
        "favorite-number": 987654321,
    },
]


# functions can be used to handle recurring tag structures
def my_custom_nav():
    # these functions can return a tag or a list of tags ( [tag1,tag2,tag3] )
    return Nav([Class("nav pretty")],
        Div([], "A beautiful NavBar")
    )


html = Html([],
    Head([],
        Title([], "An awesome site")
    ),
    Body([],
        my_custom_nav(), # calling previously defined function
        [Div([Class(f"user-{user['name'].lower()}")],
            Div([], user['name']),
            Ul([],
                [Li([], movie) for movie in user["movies"]] # list comprehensions can be used to easily render multiple tags
            ) if user['favorite-number'] < 100 else "Favorite number is too high" # python's ternary operation is allowed too
        ) for user in users], 
        Footer([], "My Footer"),
    )
)

print(html.render(pretty=True, doctype=True)) # pass doctype=True to add a document declaration

Output

    <!DOCTYPE html>
    <html>
      <head>
        <title>
          An awesome site
        </title>
      </head>
      <body>
        <nav class='nav pretty'>
          <div>
            A beautiful NavBar
          </div>
        </nav>
        <div class='user-jose'>
          <div>
            Jose
          </div>
          <ul>
            <li>
              A beautiful mind
            </li>
            <li>
              Red
            </li>
          </ul>
        </div>
        <div class='user-jaime'>
          <div>
            Jaime
          </div>
          <ul>
            <li>
              The breakfast club
            </li>
            <li>
              Fight club
            </li>
          </ul>
        </div>
        <div class='user-jhon'>
          <div>
            Jhon
          </div>
          Favorite number is too high
        </div>
        <footer>
          My Footer
        </footer>
      </body>
    </html>

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

htmlBuilder-1.0.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

htmlBuilder-1.0.0-py2.py3-none-any.whl (14.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file htmlBuilder-1.0.0.tar.gz.

File metadata

  • Download URL: htmlBuilder-1.0.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1

File hashes

Hashes for htmlBuilder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4eae01d8acc77213328659b6be32e357f47ae10939959fcdcce8afb9393de564
MD5 bef39a2c1a74b87d1aee2977eec61ced
BLAKE2b-256 45a1da87d642b31831fb6731cbec6c5dceb3f770dd0b147beefbb1426eb8f0c6

See more details on using hashes here.

File details

Details for the file htmlBuilder-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: htmlBuilder-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1

File hashes

Hashes for htmlBuilder-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d023c1be4e1f8b0df31a3414ab27be2d44defc1ff96c242962c1d67e38091701
MD5 1e2a718e11320e6791536b0af337c35e
BLAKE2b-256 1073eb44d24c8554c7093f5b4cacd7a258c8012601727f053ea88e131888febf

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