Skip to main content

Minimal library to modify and render HTML.

Project description

A minimal Python library to manipulate and render HTML, without any templating language.

Un-template (ntpl) works with plain HTML documents. You use Python datastructures to build new document fragments. The declarative HTML manipulation works like front-end libraries like React and Mithril.

from ntpl import slurp, replace, replaceWith, render

template = slurp("index.html")
faqs = markdown(slurp("FAQ.md"))

def faq(request):
    html = template
    html = replace(html, "#content-title", "FAQ")
    html = replace(html, "#content", faqs)
    html = replaceWith(html, "a#home", render(["a", {"href": "/"}, "home"]))
    return HttpResponse(html)

To modify an existing HTML document, you pass a string containing html into the functions replace, remove, replaceWith, and attr and then use a CSS-style selector to choose the HTML elements that you want to modify. You generate HTML components from scratch by passing Python datastructures to the declarative render function. You can combine the manipulation and rendering functions to update an existing HTML document with new content.

If you're writing a back end web service you can use this library to modify pure HTML files on the server side and then pass the result back to the browser directly in the HTTP response.

Inspired by Clojure's Enlive and Hiccup, this library uses pyhiccup and Beautiful Soup to do its work.

This library works in Python 2.7 and higher.

Documentation

Generated from pydoc ntpl.

attr(html, selector, key, value=None)

Get or set value on attribute called key, in html elements that match selector.

attrs(html, selector, key)

Get a list of values of attribute called key in html elements that match selector.

remove(html, selector)

Remove html elements that match selector.

replace(html, selector, new)

Replace innerHTML of html elements that match selector with new.

replaceWith(html, selector, new)

Replace outerHTML of html elements that match selector with new.

slurp(path)

Load a file as a string.

select(html, selector)

Select one or more fragments matching selector and return them as an HTML string.

Example

import ntpl

# load an HTML file in as a string
t = ntpl.slurp("index.html")

# get the ids of all divs
ntpl.attrs(t, "div", "id")
# ['first', 'second']

# set the class of a div with id="frank"
ntpl.attrs(t, "div#frank", "class", "special")
# returns rendered HTML

# remove all <li> elements
ntpl.remove(t, "li")
# returns rendered HTML with <li> elements removed

# replace the innerHTML of a p element with id "description"
ntpl.replace(t, "p#description", "Some new stuff.")
# returns rendered HTML

# replace the entire outerHTML of a p element
ntpl.replaceWith(t, "p", "<h2>Barre.</h2>")
# returns rendered HTML with all <p> replaced with new tag

# render Python datastructure as HTML
ntpl.render(["div", {"class": "special"}, "Some text."])
# <div class="special">Some text.</div>
# see https://github.com/nbessi/pyhiccup for details

# combine HTML manipulation with rendering
ntpl.replace(t, "ul#users",
    ntpl.render([["li", "Bob"], ["li", "Alice"], ["li", "Satoshi"]]))
# returns render HTML document with user list items contents replaced

Who

Hi, 👋 I'm Chris and I made this.

You can find me online here:

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

ntpl-0.0.4.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file ntpl-0.0.4.tar.gz.

File metadata

  • Download URL: ntpl-0.0.4.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/2.7.17

File hashes

Hashes for ntpl-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d0daf0ef6eeb65ecf67d7333b05bd11bf409f317051e9ef9e65858e550086cb5
MD5 55bee4aa87e218f5936f70d2a62222bd
BLAKE2b-256 3772be12c0e95be7555486b7c1ea2111c830ccfb0bb447d1ff35b389db0a6495

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