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

Pure-Python HTML generation

Project Description

Pure-Python HTML generation, inspired by [Hiccup][1].

from cottonmouth.html import render
from cottonmouth.tags import html, head, body, title, meta, link, h1

def welcome(user=None, **context):
return ['p', 'Welcome' + (' back!' if user else '!')]

content = (
# Feel free to use raw HTML
'<!doctype html>',
# Tags are represented as sequences with a tag name at the head
# Or just use strings instead of the default tag symbols
[title, 'The Site'],
# Attributes are passed as a dict immediately after the tag
[meta, {'charset': 'utf-8'}],
[link, dict(rel='stylesheet', type='text/css',
# You can also call tags as functions with the content as
# the first argument and attributes as `kwargs`
[header, h1(u'The Website', id='header')],
# Use "#id.class" shortcuts to easily create `div` elements
# Functions will be called with context and the results rendered
['#main', welcome]]]

render(*content, user=None)

Equivalent output:

<!doctype html>
<title>The Site</title>
<meta content="text/html;charset=utf-8" http-equiv="content-type">
<link href="static/layout.css" type="text/css" rel="stylesheet">
<h1 id="header">Welcome to the site!</h1>
<div id="map" class="pretty-map"></div>
<div id="main">

### Classes

There's a shortcut for dynamic classes: strings or iterables of classnames are automatically appended to the sugared classname!

render(['.foo', {'class': 'bar baz'}])
# u'<div class="foo bar baz"></div>'

render(['.foo', {'class': ['bar', 'baz']}])
# u'<div class="foo bar baz"></div>'

### Installation

pip install cottonmouth

### Testing

python test

### License

BSD Copyright 2013, Noah Seger

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


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
cottonmouth-0.2.2.tar.gz (4.6 kB) Copy SHA256 Checksum SHA256 Source Jan 18, 2017

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