Skip to main content
Warning Some features may not work without JavaScript. Please try enabling it if you encounter problems.

sphc 1.0.1

pip install sphc

Latest version

Released:

Simple Pythonic HTML Creator

Verified details

These details have been verified by PyPI
Maintainers
Avatar for shon from gravatar.com shon

Unverified details

These details have not been verified by PyPI
Project links
Meta
  • License: MIT License (MIT)
  • Author: Shekhar Tiwatne
  • Tags html , pythonic , html-generator , web-development
  • Requires: Python >=3

Project description

Simple Pythonic HTML Creator (sphc) ๐Ÿโœจ

A super simple and Pythonic way to create HTML, for Python 3.

  • โœ… Ultra simple and it just works!
  • ๐Ÿ Pythonic syntax you'll love.
  • ๐Ÿš€ Modern and built for Python 3.
>>> fromsphcimport tf
>>> adiv = tf.DIV("Hello World!", id="header_1", Class="header")
>>> print(adiv)
<DIV id="header_1" Class="header">Hello World!</DIV>

๐Ÿ“– It's super easy to learn!

Hello World!

>>> importsphc
>>> tf = sphc.TagFactory()
>>> header = tf.H1("Hello World!")
>>> print(header)
<H1>Hello World!</H1>

Constructing a Page ๐Ÿ“„

>>> doc = tf.HTML()
>>> doc.body.content = tf.H1("The content")
>>> print(doc)
<HTML><BODY><H1>The content</H1></BODY></HTML>

Using Lists of Tags ๐Ÿ“‹

Especially useful for constructing tables and select options.

>>> data = [('One', '1'), ('Two', '2'), ('Three', '3')]
>>> atable = tf.TABLE()
>>> for element in data:
... row = tf.TR()
... row.cells = [tf.TD(element[0]), tf.TD(element[1])]
... atable.row = row
>>> print(atable)
<TABLE><TR><TD>One</TD><TD>1</TD></TR><TR><TD>Two</TD><TD>2</TD></TR><TR><TD>Three</TD><TD>3</TD></TR></TABLE>

Wrapping Content ๐ŸŽ

>>> block1 = tf.DIV(tf.DIV("content", Class="inner"), Class="outer")
>>> block2 = tf.DIV([tf.DIV(), tf.DIV()], Class="outer")
>>> content = tf.DIV([block1, block2])

Chaining Methods โ›“๏ธ

The set_required method sets the required property on a Tag object AND returns the Tag object, so you can chain calls.

>>> form = tf.FORM()
>>> form.username = tf.INPUT(name="username").set_required()
>>> print(form)
<FORM><INPUT name="username" required/></FORM>

Properties with No Value โ˜‘๏ธ

>>> c = tf.INPUT(None, 'checked', type='checkbox', value='foo')
>>> print(c)
<INPUT type="checkbox" value="foo" checked/>

Escaping Content ๐Ÿ”“

By default, content is escaped to prevent XSS attacks.

>>> print(tf.C(' >> ')) # Default
<C> &gt;&gt; </C>
>>> print(tf.C(' >> ', escape=False))
<C> >> </C>

๐Ÿš€ More Experimental Stuff

Ready for more? Let's dive into some advanced features in sphc.more.

Hello sphc.more ๐Ÿ‘‹

>>> importsphc.more
>>> tf = sphc.TagFactory()
>>> classMyPage(sphc.more.HTML5Page):
... deffooter(self):
... return tf.FOOTER("Footer text")
>>> my_page = MyPage()
>>> my_page.render()

This will return a complete HTML5 page as a string, just like you'd expect.

Building a Form ๐Ÿ“

>>> importsphc
>>> importsphc.more
>>>
>>> tf = sphc.TagFactory()
>>>
>>> form = sphc.more.Form(classes=['vform'])
>>> form.add_field('Username', tf.INPUT(type="TEXT", id='username', name="username").set_required())
>>> form.add_field('Password', tf.INPUT(type="password", id='password', name="password"))
>>> form.add_buttons(tf.BUTTON("Log In", id='login-btn', type='button'))
>>> print(form.build())
<FORM method="POST" Class="vform"><DIV Class="field"><DIV Class="field-label"><LABEL For="username">Username</LABEL></DIV><DIV Class="field-input"><INPUT type="TEXT" id="username" name="username" required/><C> *</C></DIV></DIV><DIV Class="field"><DIV Class="field-label"><LABEL For="password">Password</LABEL></DIV><DIV Class="field-input"><INPUT type="password" id="password" name="password"/></DIV></DIV><DIV Class="action-status"></DIV><DIV Class="buttons"><BUTTON id="login-btn" type="button">Log In</BUTTON></DIV></FORM>

Forms with Fieldsets ๐Ÿ—‚๏ธ

>>> form = sphc.more.Form()
>>>
>>> about = form.add(sphc.more.Fieldset())
>>> about.add(sphc.tf.LEGEND('About'))
>>> about.add_field('Name', sphc.tf.INPUT(name='name', type='text'))
>>>
>>> contact = form.add(sphc.more.Fieldset())
>>> contact.add(sphc.tf.LEGEND('Contact'))
>>> contact.add_field('Email', sphc.tf.INPUT(name='email', type='email'))
>>> print(form.build())
<FORM method="POST"><FIELDSET><LEGEND>About</LEGEND><DIV Class="field"><DIV Class="field-label"><LABEL For="form-name">Name</LABEL></DIV><DIV Class="field-input"><INPUT name="name" type="text" id="form-name"/></DIV></DIV></FIELDSET><FIELDSET><LEGEND>Contact</LEGEND><DIV Class="field"><DIV Class="field-label"><LABEL For="form-email">Email</LABEL></DIV><DIV Class="field-input"><INPUT name="email" type="email" id="form-email"/></DIV></DIV></FIELDSET></FORM>

๐Ÿ’ป Source

Find the source code on GitHub: https://github.com/shon/sphc

๐Ÿ› ๏ธ Building from Source

For instructions on how to build and distribute the package from source, please see BUILD.md.


๐Ÿš€ Performance

A small, informal benchmark was conducted to compare sphc with some other popular template/html generation libraries. The benchmark generates a 100-row HTML table 1000 times. The results below show the time taken in seconds (lower is better).

Please note that these benchmarks are not comprehensive and your results may vary depending on the specific use case and hardware.

Library Time (seconds) Chart (longer is slower)
Mako 1.81 โ–ˆโ–ˆโ–ˆโ–ˆ
Jinja2 4.08 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
sphc 11.33 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
dominate 17.26 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ

These results were generated on a generic cloud instance and should be taken with a grain of salt.

Project details

Verified details

These details have been verified by PyPI
Maintainers
Avatar for shon from gravatar.com shon

Unverified details

These details have not been verified by PyPI
Project links
Meta
  • License: MIT License (MIT)
  • Author: Shekhar Tiwatne
  • Tags html , pythonic , html-generator , web-development
  • Requires: Python >=3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphc-1.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

sphc-1.0.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file sphc-1.0.1.tar.gz.

File metadata

  • Download URL: sphc-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for sphc-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2ab8de7ee46bff5cbc80a85de14251ca3950e0ec5d0675a71c5d6d166eb54cae
MD5 7ec8462f7ebf0093f2e7828a5e23ce45
BLAKE2b-256 769409486a8571a8740193500c7f22338a897cb9d3271d1f05aa853c80d33e01

See more details on using hashes here.

File details

Details for the file sphc-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sphc-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for sphc-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 647cbd8e1104bd4865e68c607429063429210ea327069ef909705a14eddfb355
MD5 5acea42b36476a85f60e0db1ee94e7c7
BLAKE2b-256 323ee8690be84e89feceeda112bb931c8589c019f0fb2cfd509a3b1ccb5e24e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /