Simple Pythonic HTML Creator
Project description
Ultra simple and works
Compatible with Python3/ 2.x
Source: https://github.com/shon/sphc
Any suggestions/issues | https://github.com/shon/sphc/issues
Critisism or if you feel such thing already implemented feel free to write Authour.
As simple as below:
>>> import sphc >>> tf = sphc.TagFactory() >>> html = tf.HTML() >>> html.head = tf.HEAD() >>> html.body = tf.BODY() >>> html.body.content = tf.DIV("Some Text here.", Class='content') >>> html.body.content.br = tf.BR() >>> html.body.content.br = tf.BR() >>> html.footer = tf.FOOTER() >>> 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 >>> more_cells = [tf.TD('Four'), tf.TD('4')] >>> row = tf.TR() >>> row.cells = more_cells >>> atable.row = row >>> html.body.content.atable = atable >>> html.body.content.attributes['id'] = 'content_id' >>> print(html) >>> print(html.pretty())
Remember
pretty method should only be used for debugging. Real output is generated by str( <tag-object> )
Properties with no value required
>>> c = tf.INPUT(nv_attrs=['checked'], type='checkbox', value='foo') >>> print(c) <INPUT checked type="checkbox", value="foo"/>
Escaping
>>> print(tf.C(' >> ')) # Default >>> <C> >> </C> >>> print(tf.C(' >> ', escape=False)) >>> <C> >> </C>
More
More (sphc.more) provides some helper widgets based on core sphc. sphc.more usable but in progress work. You may want to use but be ready for api changes.
Base HTML5 page classes
(loosely based on html5boilerplate.com templates):
>>> import sphc.more >>> tf = sphc.TagFactory() >>> class MyPage(sphc.more.HTML5Page): def footer(self): return tf.FOOTER("Footer text") >>> my_page = MyPage() >>> my_page.render()
Building a form
>>> import sphc >>> import sphc.more >>> >>> tf = sphc.TagFactory() >>> >>> form = sphc.more.Form(classes=['vform']) >>> form.add_field('Username', tf.INPUT(type="TEXT", id='username', name="username", placeholder="Username")) >>> form.add_field('Password', tf.INPUT(type="password", id='password', name="password", placeholder="Password")) >>> form.add_buttons(tf.BUTTON("Log In", id='login-btn', type='button')) >>> print form.build()
output:
<FORM Class="vform"> <DIV Class="field" For="username"> <DIV> <LABEL Class="field-label">Username</LABEL> </DIV> <INPUT placeholder="Username" Class="field-input" type="TEXT" name="username" id="username"/> </DIV> <DIV Class="field" For="password"> <DIV> <LABEL Class="field-label">Password</LABEL> </DIV> <INPUT placeholder="Password" Class="field-input" type="password" name="password" id="password"></INPUT> </DIV> <DIV Class="buttons"> <BUTTON type="button" id="login-btn">Log In</BUTTON> </DIV> </FORM>
Similar packages
http://pypi.python.org/pypi/html # Inspiration. It uses __getattr__ trick whereas this package is mostly a __setattr__ trick, aiming sharing of blocks.
TODO
To add more tests
A document class ?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sphc-0.8.2.tar.gz
.
File metadata
- Download URL: sphc-0.8.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c35817dcb50a61036d3c629d6c6b004f544e602120c560a3a750eb5ea460fa31 |
|
MD5 | 4d449d08e71e10366c10a39bdf97a755 |
|
BLAKE2b-256 | ce4c7b388a1907dcd8f89e13017f2b65891d850280b9071dd6cb0c136dc8471d |