python html generator

前端 未结 9 1510
轮回少年
轮回少年 2020-12-04 22:28

I am looking for an easily implemented html generator for python. I found this one

http://www.decalage.info/python/html

but there is no wa

相关标签:
9条回答
  • 2020-12-04 22:45

    This is one ultra-simple HTML generator I have written. I use it build-time to generate html. If one is generating html pages run-time then there are better options available

    Here is the link

    http://pypi.python.org/pypi/sphc

    And a quick example

    >> import sphw
    >> tf = sphw.TagFactory()
    >>> div = tf.DIV("Some Text here.", Class='content', id='foo')
    >>> print(div)
    
    <DIV Class="content", id="foo">Some Text here.</DIV>
    
    0 讨论(0)
  • 2020-12-04 22:46

    If you want programmatic generation rather than templating, Karrigell's HTMLTags module is one possibility; it can include e.g. the class attribute (which would be a reserved word in Python) by the trick of uppercasing its initial, i.e., quoting the doc URL I just gave:

    Attributes with the same name as Python keywords (class, type) must be capitalized :

    print DIV('bar', Class="title")  ==>  <DIV class="title">bar</DIV>
    
    0 讨论(0)
  • 2020-12-04 22:47

    HTML Generation is usually done with one of the infinite amounts of HTML templating languages available for Python. Personally I like Templess, but Genshi is probably the most popular. There are infinite amounts of them, there is a list which is highly likely to be incomplete.

    Otherwise you might want to use lxml, where you can generate it in a more programmatically XML-ish way. Although I have a hard time seeing the benefit.

    0 讨论(0)
  • 2020-12-04 22:49

    Dominate is an HTML generation library that lets you easily create tags. In dominate, python reserved words are prefixed with an underscore, so it would look like this:

    from dominate.tags import *
    t = div(table(_id="the_table"), _class="tbl")
    print(t)
    
    
    <div class="tbl">
      <table id="the_table"></table>
    </div>
    

    Disclaimer: I am the author of dominate

    0 讨论(0)
  • 2020-12-04 22:49

    Ok, here's another html generator, or I prefer to think of it as a compiler.

    https://pypi.python.org/pypi/python-html-compiler

    This is a set of base classes that can be used to define tags and attributes. Thus a tag class has attributes and children. Children are themselves Tag classes that have attributes and children etc etc. Also you can set parameters that start with your root class and work up the various branches.

    This will allow you to define all the tag classes you want thus be able to create customised classes and implement any tags or attributes you want.

    Just started on this, so if anybody wants to test :)

    0 讨论(0)
  • 2020-12-04 22:56

    You might be interested in some of the Python HAML implementations. HAML is like HTML shorthand and only takes a few minutes to learn. There's a CSS version called SASS too.

    http://haml.hamptoncatlin.com/

    "Is there a HAML implementation for use with Python and Django" talks about Python and HAML a bit more.

    I'm using HAML as much as possible when I'm programming in Ruby. And, as a footnote, there's also been some work getting modules for Perl which work with the nice MVC Mojolicious:

    http://metacpan.org/pod/Text::Haml

    0 讨论(0)
提交回复
热议问题