Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A simple Python HTML generator

Project Description

HtmlNode is an internal Domain Specific Language (DSL) using Python to generate HTML templates. It is designed to be really easy and flexible to use. By using DSL instead of a specific templating language, you can write Python code to render HTML directly, so you can debug your presentation logics easily. Also, you get the full power of Python in writing presentation logic, without the hassle to learn another templating language.

An overview for using internal DSL vs external template languages can be found here.

Installation

Automatic installation:

pip install HtmlNode

HtmlNode is listed in PyPI and can be installed with pip or easy_install.

Manual installation: Download the latest source from PyPI.

tar xvzf HtmlNode-$VERSION.tar.gz
cd HtmlNode-$VERSION
sudo python setup.py install

The HtmlNode source code is hosted on GitHub.

Prerequisites: HtmlNode only runs on Python 2.7 currently.

Features

  • Very simple syntax and natural way to generate HTML
  • Unicode support
  • Auto-escape dangerous characters
  • Template inheritance support
  • Context variables insertion into templates
  • Auto-completion support for IDEs which can introspect
  • Custom HTML elements can be created on the fly
  • Flexible ways to include attributes for HTML element

Usage Example

Example 1 - Simple HTML:

Code snippet:

from html_node import html_node as h

template = h.div(class_='container')(
                h.span('Hello World!', style='color: pink; font-weight: bold;')
           )
print template

Output:

<div class="container">
    <span style="color: pink; font-weight: bold;">
        Hello World!
    </span>
</div>

By default, the actual output is concatenated without space or line feeds.

Example 2 - Simple reusable layout (with inheritance and placeholders)

You can use placeholder to mark different block section to replace with. You give the placeholder a name, and provide a method with the same name that returns a unicode string. You can inherit the layout just like how you normally do in Python.

Code snippet:

from html_node import html_node as h, BaseTemplate, placeholder


class MyLayout(BaseTemplate):
    """My customized layout which other templates can inherit from."""

    title = 'HtmlNode'

    def template(self):
        return h.html5(placeholder('head'), placeholder('body'))

    @placeholder
    def head(self):
        return h.head(
                   h.title(self.title),
                   h.link(rel="stylesheet", type="text/css", href="default.css"),
               )

    @placeholder
    def body(self):
        return h.body(
                   h.div(class_=['container', 'expand'])(
                       placeholder('content')
                   )
               )

    @placeholder
    def content(self):
        raise NotImplementedError


class MyViewTemplate(MyLayout):
    """This template will provide the content block for the layout."""

    @placeholder
    def content(self):
        return u'HtmlNode is a flexible and easy-to-use HTML generator!'


template = MyViewTemplate()
print template

Output:

<!DOCTYPE HTML>
<html>
    <head>
        <title>HtmlNode</title>
        <link href="default.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <div class="container expand">
            HtmlNode is a flexible and easy-to-use HTML generator!
        </div>
    </body>
</html>
Release History

Release History

This version
History Node

0.1.8

History Node

0.1.7

History Node

0.1.6

History Node

0.1.5

History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
HtmlNode-0.1.8.tar.gz (8.1 kB) Copy SHA256 Checksum SHA256 Source Oct 27, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting