HTML 5发电机

htmlgen的Python项目详细描述


https://img.shields.io/pypi/l/htmlgen.svghttps://img.shields.io/github/release/srittau/python-htmlgen/all.svghttps://img.shields.io/pypi/v/htmlgen.svghttps://travis-ci.org/srittau/python-htmlgen.svg?branch=master

从类生成HTML的库。

基本用法:

>>> from htmlgen import Division, Span
>>> Division("This is ", Span("important!"), "!")

更详细的示例:

>>> span = Span("important")
>>> span.add_css_classes("important")
>>> div = Division()
>>> div.id = "my-block"
>>> div.append("This is ")
>>> div.append(span)
>>> div.append("!")

这样构造的树可以转换为字符串:

>>> str(div)
'<div id="my-block">This is <span class="important">important</span>!</div>'
>>> "<p>This is {}!</p>".format(span)
'<p>This is <span class="important">important</span>!</p>'

或者,所有元素都可以用作迭代器,例如 它们来自wsgi回调:

>>> def application(env, start_response):
...     start_response("200 OK", [("Content-Type", "text/html")])
...     return div

有两种不同的方式来呈现html元素的子元素。那棵树 上述施工方法主要适用于 孩子们。这种方法的缺点是整个树必须 建立在记忆中。另一种方法,最适合自定义子类 ,重写元素类的generate_children方法:

>>> class MyBlock(Division):
...     def __init__(self):
...         super(MyBlock, self).__init__()
...         self.id = "my-block"
...     def generate_children(self):
...         yield "This is "
...         span = Span("important")
...         span.add_css_classes("important")
...         yield span
...         yield "!"
>>> str(MyBlock())
'<div id="my-block">This is <span class="important">important</span>!</div>'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java FloatingAction按钮与RecyclerView中的CardView重叠   java如何计算CardLayout中的卡数   从远程系统上传MySQL数据库并访问Java应用程序   java调用堆栈如何处理带或不带返回类型的递归?   Springboot中的java组计数聚集   java如何在javafx textarea中使用richtextfx   获取与Mockito相关的错误时出现Java问题   java如何将JaxRS响应转换为Wiremock响应   Hadoop集群java。net ConnectionException:连接被拒绝错误   java如何加载文件私有文件类型是pem   java在元空间中的提升和加载的类   如何将系统属性传递给从HTML启动的Java小程序   java如何从网页中获取值并在主类中使用它?安卓应用   java在春天,advisor和aspect之间有什么区别?   java如何检测文件是否已重命名?   java消息驱动Bean何时使用