HTML generation library for Python
Python
Switch branches/tags
Nothing to show
Latest commit 23b0cd1 May 30, 2017 @cenkalti cenkalti bump version
Permalink
Failed to load latest commit information.
.coveragerc fix for coveralls Dec 6, 2016
.gitignore ignore cache dirs May 28, 2017
.travis.yml fix for coveralls Dec 6, 2016
LICENSE add license Jun 2, 2013
README.rst fix for coveralls Dec 6, 2016
bench.py var Jun 19, 2013
pyhtml.py bump version May 30, 2017
setup.cfg add flake8 config May 28, 2017
setup.py fix for coveralls Dec 6, 2016
sitecustomize.py coverage Oct 26, 2016
tests.py fix multiple blocks with same name May 30, 2017
tox.ini fix doctests May 18, 2017

README.rst

PyHTML

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>