对于support both a JPEG and WEBP compressed image,我想在网页中包含以下代码:
<picture>
<source srcset="img/awesomeWebPImage.webp" type="image/webp">
<source srcset="img/creakyOldJPEG.jpg" type="image/jpeg">
<img src="img/creakyOldJPEG.jpg" alt="Alt Text!">
</picture>我一直在使用Python Dominate,它通常对我来说运行得很好。但我认为Dominate不支持Picture和Source标签。我可以将HTML添加为raw() Dominate标记,但是我想知道是否有一种方法可以让Dominate识别这些标记。
p = picture()
with p:
source(srcset=image.split('.')[0]+'.webp', type="image/webp")
source(srcset=image, type="image/jpeg")
img(src=image, alt=imagealt)我看到了这样的错误:
p = picture()
NameError: global name 'picture' is not defined发布于 2019-04-28 04:40:16
Dominate用于生成HTML(5)文档。
元素列表在tags.py文件中定义,请参阅GitHub:https://github.com/Knio/dominate/blob/master/dominate/tags.py中的存储库。
但是,picture不是一个标准的标签。
您可以查看lxml库,其中包含一个类似于Dominate的ElementMaker,可以轻松地构建XML树。请参阅E-Factory。
例如:
>>> from lxml.builder import E
>>> def CLASS(*args): # class is a reserved word in Python
... return {"class":' '.join(args)}
>>> html = page = (
... E.html( # create an Element called "html"
... E.head(
... E.title("This is a sample document")
... ),
... E.body(
... E.h1("Hello!", CLASS("title")),
... E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
... E.p("This is another paragraph, with a", "\n ",
... E.a("link", href="http://www.python.org"), "."),
... E.p("Here are some reserved characters: <spam&egg>."),
... etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
... )
... )
... )
>>> print(etree.tostring(page, pretty_print=True))
<html>
<head>
<title>This is a sample document</title>
</head>
<body>
<h1 class="title">Hello!</h1>
<p>This is a paragraph with <b>bold</b> text in it!</p>
<p>This is another paragraph, with a
<a href="http://www.python.org">link</a>.</p>
<p>Here are some reserved characters: <spam&egg>.</p>
<p>And finally an embedded XHTML fragment.</p>
</body>
</html>发布于 2020-04-14 20:17:05
您可以通过继承dominate.tags.html_tag类来创建图片类
from dominate.tags import html_tag
class picture(html_tag):
pass现在可以将其用作任何预定义的标记。
https://stackoverflow.com/questions/55884621
复制相似问题