Skip to main content

Render HTML strings from Python dictionaries

Project description

Pypertext

Transform a Python dictionary to HTML string.

Install

Requires python 3.6+

pip install pypertext

Usage

The render_element function taks a dictionary and returns a string.

el = {
    "tag": "div",
    "attrs": {
        "class": "container",
    },
    "children": [
        {
            "tag": "h1",
            "children": "Hello World!",
        },
        {
            "tag": "p",
            "children": "This is a paragraph.",
        },
    ],
}
expected = '<div class="container"><h1>Hello World!</h1><p>This is a paragraph.</p></div>'
out = render_element(el)
assert out == expected

The render_document function takes a dictionary or list and returns a full HTML document as a string.

el = {
    "tag": "div",
    "attrs": {
        "class": "container",
    },
    "children": [
        {
            "tag": "h1",
            "children": "Hello World!",
        },
    ],
}
expected = '<!DOCTYPE html><html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/></head><body><div class="container"><h1>Hello World!</h1></div></body></html>'
out = render_document(body=el)
assert out == expected

Tasks

Use the xc task runner to execute the commands below.

build-dist

rm -rf ./dist
python setup.py sdist

clean

Delete build artifacts

rm -rf dist/ pypertext.egg-info/ site/ build/
rm -rf ./pypertext/*.so
rm -rf ./pypertext/__pycache__/
rm -rf ./pypertext_sessions.db

upload-pypi

twine upload --repository pypi dist/*

upload-testpypi

twine upload --repository testpypi dist/*

install-dev

rm -rf ./pypertext.egg-info
python -m pip uninstall -y pypertext
python -m pip install -e .[dev]

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

pypertext-0.1.1.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file pypertext-0.1.1.tar.gz.

File metadata

  • Download URL: pypertext-0.1.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for pypertext-0.1.1.tar.gz
Algorithm Hash digest
SHA256 29c338bd2ec8eda54216e5777828f5c126b87a8ea58c8b151a8a1cc68e0883a1
MD5 b2fba265431060c16b46e59cf17d5631
BLAKE2b-256 d4a4132395ca4ca8b7fe7c2b7837711a49bf681f74df3c53e6dfbd8cbbe8d24f

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