Skip to main content

Simple Pythonic HTML Creator

Project description

  • Ultra simple and works

  • Compatible with Python3/ 2.x

  • Pythonic
    >>> adiv = tf.DIV("Hello World!", id="header_1", Class="header")
    >>> print (adiv)
    <DIV id="header_1" class="header">Hello World</DIV>

Examples

Hello World!

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

Constructing a page

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

Using list of Tag objects

Especially usefule 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

Wrapping

>>> 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

set_required method below sets required property on Tag object AND returns Tag object:

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

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> &gt;&gt; </C>

>>> print(tf.C(' >> ', escape=False))
>>> <C> >> </C>

More

Well since you reached here time to show some experimental stuff.

Hello sphc.more

(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()

This will return a string that would contain html exactly like what you have expected

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").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 required type="TEXT" name="username" id="username"></INPUT><C>*</C>
        </DIV>
    </DIV>
    <DIV Class="field">
        <DIV Class="field-label"> <LABEL For="password">Password</LABEL></DIV>
        <DIV Class="field-input">
            <INPUT type="password" name="password" id="password"></INPUT>
        </DIV>
    </DIV>
    <DIV Class="action-status"></DIV>
    <DIV Class="buttons">
        <BUTTON type="button" id="login-btn">Log In</BUTTON>
    </DIV>
</FORM>

Form 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('About'))
>>> contact.add_field('Name', sphc.tf.INPUT(name='name', type='text'))

Finally

Similar packages

TODO

  • To add more tests

  • A document class ?

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

sphc-0.9.2.tar.gz (7.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: sphc-0.9.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sphc-0.9.2.tar.gz
Algorithm Hash digest
SHA256 bdb09e5f8424baa7ba16b263427c80b62400e338a3938ab5ac8cafe96317b969
MD5 ce0d554bf498d3bf9489aff68d56015d
BLAKE2b-256 b7c8d86ba4b4c22e58cec8579227ea2de2ca4b22256586e9a64850a13e257ffa

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