Python 中的 HTML 生成

发布于 2024-09-07 18:24:05 字数 216 浏览 2 评论 0原文

在 Python 中快速创建简单 HTML 的最简单方法是什么?到目前为止,我发现的只是复杂的模板系统或用于 HTML 生成的类,其 API 似乎比我需要的要重得多。

我可以自己通过将字符串粘在一起来完成,但我认为可能有一个库可以节省我一点时间。

编辑-我决定仔细研究一下 Jinja2 模板系统。看起来它在未来的某个时候可能会有用,而且看起来并不像我想象的那样需要花太多时间来弄清楚。

What's the easiest way to quickly create some simple HTML in Python? All I've found so far are complex templating systems or classes for HTML generation with APIs that seem much heavier than what I need.

I could just do it myself by sticking strings together but I thought there might be a library that could save me a little time.

edit- I decided to take a closer look at the Jinja2 templating system. It looks like it could be useful at some point in the future and doesn't look like it will take as much time to figure out as I thought.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木落 2024-09-14 18:24:05

quixote 是一个古老但仍然令人着迷的框架——它基本上“嵌入 HTML在 Python 中”(而不是像所有流行的模板系统那样反之亦然)。

概述中的一个简单示例:

def format_row [html] (head, value):
    "<tr valign=top align=left>\n"
    "  <th align=left>%s</th>\n" % head
    "  <td>%s</td>\n" % value
    "</tr>\n"

在 Python 本身中,第一个字符串将是文档字符串,其他部分将被忽略,并且 [html] 部分将是语法错误。在Quixote中,[html]将此函数标记为“PTL”(Python模板语言)而不是Python本身,具有此类函数的模块使用的文件扩展名是.ptl,但它们仍然可以从 Python 中导入并输出这些字符串。

我怀疑您想要采用 Quixote 而不是现代 Python 模板方法,但恕我直言,它确实会让阅读变得有趣。

类似的还有 nevow (尽管它更适合生成 XML,而不是 HTML 本身) ,特别是。 stan,典型的例子是......:

>>> from nevow import flat, stan
>>> html = stan.Tag('html')
>>> p = stan.Tag('p')
>>> someStan = html[ p(style='font-family: Verdana;')[ "Hello, ", "world!" ] ]
>>> flat.flatten(someStan)
'<html><p style="font-family: Verdana;">Hello, world!</p></html>'

有点“甚至更酷”...因为您不必担心正确关闭标签;-)。

但最后,对于生产工作模板系统,如 jinja2mako 如今通常是首选 - 主要的实际原因是将表示逻辑(在模板中)与其他层(在 Python 中)更好地分离我猜,他们提供了“在 Python 中嵌入 HTML/XML”方法。

quixote is an old-ish but still fascinating framework -- it basically "embeds HTML in Python" (rather than vice versa, as all popular templating systems do).

One simple example from the overview:

def format_row [html] (head, value):
    "<tr valign=top align=left>\n"
    "  <th align=left>%s</th>\n" % head
    "  <td>%s</td>\n" % value
    "</tr>\n"

In Python proper, the first of those strings would be the docstring, the others would be ignored, and the [html] part would be a syntax error. In Quixote, the [html] marks this function as being "PTL" (Python Template Language) rather than Python proper, the file extension to use for modules with such functions is .ptl, but they can still be imported from Python and those strings are output.

I doubt you want to adopt Quixote in preference to modern Python templating approaches, but it does make for interesting reading, IMHO.

Further along similar lines is nevow (though it's more geared to generating XML, not HTML per se), esp. stan, where the canonical example is...:

>>> from nevow import flat, stan
>>> html = stan.Tag('html')
>>> p = stan.Tag('p')
>>> someStan = html[ p(style='font-family: Verdana;')[ "Hello, ", "world!" ] ]
>>> flat.flatten(someStan)
'<html><p style="font-family: Verdana;">Hello, world!</p></html>'

Kind of "even cooler"... because you don't have to worry about closing tags correctly;-).

In the end, though, for production work templating systems like jinja2 or mako are typically preferred these days -- the main practical reason being the better separation of presentation logic (in the template) from other layers (in Python code proper) than they offer wrt the "embed HTML/XML within Python" approaches, I guess.

玩物 2024-09-14 18:24:05

看一下标记模块: http://markup.sourceforge.net/

编辑:这是另一个模块看起来很相似: http://www.decalage.info/en/python/html

Have a look at the Markup module: http://markup.sourceforge.net/

Edit: Here's another module that looks quite similar: http://www.decalage.info/en/python/html

泛泛之交 2024-09-14 18:24:05

Python string.Template 可以做你想做的事。而且它是内置的。

http://docs.python.org/library/string.html#template- strings

如果没有进一步的定义或示例,我无法理解“看起来更重的 API”。

The Python string.Template may do what you want. And it's built-in.

http://docs.python.org/library/string.html#template-strings

I can't understand "APIs that seem much heavier" without further definition or examples.

陪我终i 2024-09-14 18:24:05

如果您正在寻找基本的准系统,那么古老的 HTMLgen 就可以了。否则,请使用模板引擎。

If you're looking for basic and barebones then the ancient HTMLgen will do. Otherwise, use a template engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文