我正在尝试通过yattag创建html。唯一的问题是,我从另一个html文件中使用了头文件,因此我读取了该文件并尝试将其作为头插入。这是问题所在。尽管我传递了未转义的html字符串,但yattag对其进行了转义。也就是说,它在添加到html字符串时将'<'转换为<
。
MWE:
from yattag import Doc, indent
import html
doc, tag, text = Doc().tagtext()
h = open(nbheader_template, 'r')
h_content= h.read()
h_content = html.unescape(h_content)
doc.asis('<!DOCTYPE html>')
with tag('html'):
# insert dummy head
with tag('head'):
text(h_content) # just some dummy text to replace later - workaround for now
with tag('body'):
# insert as many divs as no of files
for i in range(counter):
with tag('div', id = 'divID_'+ str(1)):
text('Div Page: ' + str(i))
result = indent(doc.getvalue())
# inject raw head - dirty workaround as yattag not doing it
# result = result.replace('<head>headtext</head>',h_content)
with open('test.html', "w") as file:
file.write(result)
上下文:我试图将多个jupyter python笔记本组合到一个html中,这就是为什么标题很重的原因。标头内容(nbheader_template)可以找到here
答案 0 :(得分:1)
答案 1 :(得分:0)
我对'yattag'并不十分流利,但是我发现缺少的一件事是:
with tag('body'):
您上面引用的代码是将<div>
和文本放入标题中,而标题显然不属于>