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:
Metadata
Release files for ntpl 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ntpl-0.0.4.tar.gz | 3.2 kB | Without provenance | Details for ntpl-0.0.4.tar.gz |
Release files / ntpl-0.0.4.tar.gz
| Download URL | ntpl-0.0.4.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d0daf0ef6eeb65ecf67d7333b05bd11bf409f317051e9ef9e65858e550086cb5
|
|
BLAKE2b-256 checksum How to use checksums |
3772be12c0e95be7555486b7c1ea2111c830ccfb0bb447d1ff35b389db0a6495
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for ntpl 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.