Skip to main content

Simple package that helps with creating html strings.

Project description

This is a package which was inspired by the annoyance of writing html strings when working with serving modules like flask.

pip install htmlhelpers

Basic usage

First, lets make some strings with basic html elements.

from htmlhelpers import htmlstring as hs

Create an html element:

hs.p("We love volleyball!")
# <p>We love volleyball!</p>

or one with attributes:

hs.p("We love volleyball!", attr_str='style="text-align:right"')
# <p style="text-align:right">We love volleyball!</p>

Or build a hierarchy:

hs.html(hs.p("Still love volleyball!"))
# <html><p>Still love volleyball!</p></html>

Build a list:

hs.ul("".join[hs.li("cats"), hs.li("dogs")])
# <ul><li>cats</li><li>dogs</li></ul>

Although once a list like that begins to grow, things can start to look a little messy. For more complex html expressions, we will be using the notion on a tag_series and a tag_phrase.

from htmlhelpers import htmlphrase as hp

Say we have a rappidly growing list. We can use a series to quickly create many of the same html elements in succession. We can use a tag_series.

list_of_animals = ["cats", "dogs", "rabbits"]
animals = hp.tag_series(hs.li, list_of_animals)
hs.ul(animals)
# <ul><li>cats</li><li>dogs</li><li>rabits</li></ul>

As the structure becomes deeper, we can further simplify our code with a tag_phrase.

hp.tag_phrase([hs.html, hs.div, hs.ul], animals)
# <html><div><ul><li>cats</li><li>dogs</li><li>rabits</li></ul></div></html>

Lastly, you can format the string in a much more human readable way, by setting the formatting=True in the tag_series function.

hp.tag_phrase([hs.html, hs.div, hs.ul], animals, formatting=True)
"""
<html>
  <div>
    <ul>
      <li>cats</li>
      <li>dogs</li>
      <li>rabits</li>
    </ul>
  </div>
</html>
"""

Optionally, you can pass one of these html phrase strings (any multilevel heirarchy) into the format_phrase function, found in htmlhelpers.htmlformat

Hope this helps at least a little! :)

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

htmlhelpers-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

htmlhelpers-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: htmlhelpers-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.7.3 Darwin/19.6.0

File hashes

Hashes for htmlhelpers-0.1.1.tar.gz
Algorithm Hash digest
SHA256 57ba307d72c190f53b1c329c8a7162200be337639de234d205a4f2820c391da3
MD5 bb1c46acc78d1ef0fd3e82c682548924
BLAKE2b-256 c38f06ef70e8543753c9f4843a44baf59b501893087f53e7f4e68fc8602fc491

See more details on using hashes here.

File details

Details for the file htmlhelpers-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: htmlhelpers-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.7.3 Darwin/19.6.0

File hashes

Hashes for htmlhelpers-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5af996122509b1517ab6087f18a602415c116d75acda51aa6db4abade36dfd72
MD5 3392d1fbdadbf0f92c185a20059b9119
BLAKE2b-256 72f2641b0d789072e3b219fc82bffda6fb6b960d27593548798ed7f356869eaa

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