cenkalti / pyhtml

HTML generation library for Python
Other
72 stars 10 forks source link

PyHTML

.. image:: https://coveralls.io/repos/github/cenkalti/pyhtml/badge.svg?branch=master :target: https://coveralls.io/github/cenkalti/pyhtml?branch=master

PyHTML is a simple HTML generation library for Python.

Inspired by Flask-HTMLBuilder <http://majorz.github.com/flask-htmlbuilder/> and this gist <https://gist.github.com/3516334>.

Features

Installing

.. code-block:: bash

$ pip install pyhtml

Documentation

See the docstring on pyhtml.py file.

Example

.. code-block:: python

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:

.. code-block:: html

<!DOCTYPE html>
<html>
  <head>
    <title>
      Awesome website
    </title>
    <script src="http://path.to/script.js" type="text/javascript"></script>
  </head>
  <body>
    <header>
      <img src="https://github.com/cenkalti/pyhtml/raw/master/path/to/logo.png"/>
      <nav>
        <ul>
          <li>
            <a href="https://github.com/cenkalti/pyhtml/blob/master/home.html">
              Home
            </a>
          </li>
          <li>
            <a href="https://github.com/cenkalti/pyhtml/blob/master/login.html">
              Login
            </a>
          </li>
        </ul>
      </nav>
    </header>
    <div>
      Hello Cenk
      Content here
    </div>
    <footer>
      <hr/>
      Copyright 2013
    </footer>
  </body>
</html>

Related projects