Skip to main content

Simple HTML generator for Python

Project description

https://travis-ci.org/cenkalti/pyhtml.svg?branch=master https://coveralls.io/repos/github/cenkalti/pyhtml/badge.svg?branch=master

PyHTML is a simple HTML generation library for Python.

Inspired by Flask-HTMLBuilder and this gist.

Features

  • Compatible with Python 2 and 3

  • Outputs beautifully indented code

  • Some tags have sensible defaults

  • Have Blocks for filling them later

Installing

$ pip install pyhtml

Documentation

See the docstring on pyhtml.py file.

Example

from pyhtml import *


def f_links(ctx):
    for title, page in [('Home', '/home.html'),
                        ('Login', '/login.html')]:
        yield li(a(href=page)(title))


t = html(
    head(
        title('Awesome website'),
        script(src="http://path.to/script.js")
    ),
    body(
        header(
            img(src='/path/to/logo.png'),
            nav(
                ul(f_links)
            )
        ),
        div(
            lambda ctx: "Hello %s" % ctx.get('user', 'Guest'),
            'Content here'
        ),
        footer(
            hr,
            'Copyright 2013'
        )
    )
)

print t.render(user='Cenk')

The above code is rendered as:

<!DOCTYPE html>
<html>
  <head>
    <title>
      Awesome website
    </title>
    <script src="http://path.to/script.js" type="text/javascript"></script>
  </head>
  <body>
    <header>
      <img src="/path/to/logo.png"/>
      <nav>
        <ul>
          <li>
            <a href="/home.html">
              Home
            </a>
          </li>
          <li>
            <a href="/login.html">
              Login
            </a>
          </li>
        </ul>
      </nav>
    </header>
    <div>
      Hello Cenk
      Content here
    </div>
    <footer>
      <hr/>
      Copyright 2013
    </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

PyHTML-1.3.2.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file PyHTML-1.3.2.tar.gz.

File metadata

  • Download URL: PyHTML-1.3.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for PyHTML-1.3.2.tar.gz
Algorithm Hash digest
SHA256 bae6a917854f93d5e0e021a3f3e2b3b177f5ef877998fc3e78b32cd0561f67c4
MD5 fd4e12ad0c48b1e2b09346cb3f1f757d
BLAKE2b-256 52b5ac8e2ff8748b76ee09696b29365100da1828af7f49a8614358733de2d9e8

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