python - 如何在 yattag 中添加提取的 html?
问题描述
我正在尝试通过 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)
解决方案
推荐阅读
- python - 熊猫从列中剥离字符串
- xml - XSLT 比较值并检查结果
- azure-container-instances - 我没有收到来自 azure 容器实例的任何日志,而日志位于容器选项卡中
- python - 我想阅读aa excel中的所有工作表并将其作为一个数据框
- html - 瞄准所有
- 除了 CSS 中的一个元素
- javascript - 如何将 ViewBox svg 转换为零初始坐标?
- data-structures - 如何设计此功能以在对数时间内工作?
- php - 我的 php MVC 的几个问题,路径正确
- javascript - 调用 fetch,数据不会返回,不知道为什么?
- flutter - 如何在颤动中从另一个有状态的类中调用函数?