Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.3 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67a
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Programming > Libraries

    html 1.16

    Download button

    No screenshots available
    Downloads: 464  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Richard Jones | More programs
    BSD License / FREE
    July 1st, 2011, 17:29 GMT [view history]
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    html description

    Simple, elegant HTML generation

    html offers a simple and elegant HTML generation.

    Constructing your HTML

    To construct HTML start with an instance of html.HTML(). Add tags by accessing the tag's attribute on that object. For example:

    >>> from html import HTML
    >>> h = HTML()
    >>> h.br
    >>> print h
    < br >


    If the tag should have text content you may pass it at tag creation time or later using the tag's .text() method (note it is assumed that a fresh HTML instance is created for each of the following examples):

    >>> p = h.p('hello world!
    ')
    >>> p.text('more → text', escape=False)
    >>> h.p
    >>> print h
    < p >hello, world!
    more → text< /p >
    < p >


    Any HTML-specific characters (&") in the text will be escaped for HTML safety as appropriate unless escape=False is passed. Note also that the top-level HTML object adds newlines between tags by default.

    If the tag should have sub-tags you have two options. You may either add the sub-tags directly on the tag:

    >>> l = h.ol
    >>> l.li('item 1')
    >>> l.li.b('item 2 > 1')
    >>> print h
    < ol >
    < li >item 1< /li >
    < li>< b >item 2 > 1< /b >< /li >
    < /ol >


    Note that the default behavior with lists (and tables) is to add newlines between sub-tags to generate a nicer output. You can also see in that example the chaining of tags in l.li.b. If you wished you could add attributes to those chained tags, eg: l.li(id="special").b.

    The alternative to the above method is to use the containter tag as a context for adding the sub-tags. The top-level HTML object keeps track of which tag is the current context:

    >>> with h.table(border='1'):
    ... for i in range(2):
    ... with h.tr:
    ... h.td('column 1')
    ... h.td('column 2')
    ... print h
    < table border="1" >
    < tr >< td >column 1< /td >< td >column 2< /td >< /tr >
    < tr >< td >column 1< /td >< td >column 2< /td >< /tr >
    < /table >


    Note the addition of an attribute to the tag.

    A variation on the above is to explicitly reference the context variable, but then there's really no benefit to using a with statement. The following is functionally identical to the first list construction:

    >>> with h.ol as l:
    ... l.li('item 1')
    ... l.li.b('item 2 > 1')


    You may turn off/on adding newlines by passing newlines=False or True to the tag (or HTML instance) at creation time:

    >>> l = h.ol(newlines=False)
    >>> l.li('item 1')
    >>> l.li('item 2')
    >>> print h
    < ol >< li >item 1< /li >< li >item 2< /li >< /ol >


    Since we can't use class as a keyword, the library recognises klass as a substitute:

    >>> print h.p(content, klass="styled")
    < p class="styled" >content< /p >


    How generation works

    The HTML document is generated when the HTML instance is "stringified". This could be done either by invoking str() on it, or just printing it.

    You may also render any tag or sub-tag at any time by stringifying it.

    Tags with no contents (either text or sub-tags) will have no closing tag. There is no "special list" of tags that must always have closing tags, so if you need to force a closing tag you'll need to provide some content, even if it's just a single space character.

    Rendering doesn't affect the HTML document's state, so you can add to or otherwise manipulate the HTML after you've stringified it.


    Product's homepage

    Requirements:

    · Python

    What's New in This Release: [ read full changelog ]

    · Detect and raise a more useful error when some WSGI frameworks attempt to call HTML.read(). Also added ability to add new content using the += operator.

      


    TAGS:

    HTML generation | HTML markup | Python library | HTML | generation | markup

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM