【发布时间】:2018-12-05 01:19:03
【问题描述】:
我想将 Spacy 的 Entity Recognition Visualization 渲染到 Plotly Dash 应用程序中。
用于渲染的ER Visualization的html如下:
<div class="entities" style="line-height: 2.5">
<mark class="entities" style="background: ...>
<span>...</span>
</mark>
<mark class="entities" style="background: ...>
<span>...</span>
</mark>
</div>
我尝试使用 BeautifulSoup 解析 HTML,并通过以下代码将 HTML 转换为 Dash。但是当我运行 convert_html_to_dash(html_parsed) 时,它会抛出 KeyError: 'style'
html_parsed = bs.BeautifulSoup(html, 'html.parser')
def convert_html_to_dash(el, style = None):
if type(el) == bs.element.NavigableString:
return str(el)
else:
name = el.name
style = extract_style(el) if style is None else style
contents = [convert_html_to_dash(x) for x in el.contents]
return getattr(html,name.title())(contents, style=style)
def extract_style(el):
return {k.strip():v.strip() for k,v in [x.split(": ") for x in
el.attrs["style"].split(";")]}
【问题讨论】:
标签: html plotly render spacy plotly-dash