在Python笔记本中显示HTML

时间:2020-10-28 13:09:23

标签: python html jupyter-notebook

我已经使用库scattertext生成了一个交互式散点图。散点图保存在HTML文件中。我想在.ipynb笔记本中显示它,但看起来很尴尬。 enter image description here

它应该看起来像这样: enter image description here

加上Jupyter Lab崩溃,某些元素(例如保存底部或底部运行)变为蓝色。 这是我的代码:

#Compute frequency of terms based on label
corpus = st.CorpusFromParsedDocuments(df_parser,category_col='Label', parsed_col='Parsed').build()

#Create Scatter plot in HTML
html = st.produce_scattertext_explorer(corpus,
                                    category='True',
                                    category_name='True Fact Checks',
                                    not_category_name='False Fact Checks',
                                    width_in_pixels=1000,
                                    minimum_term_frequency=5,
                                    transform=st.Scalers.scale,
                                    metadata=df_parser['Author'])

file_name = f'{PATH}Title_scatterplot.html'
open(file_name, 'wb').write(html.encode('utf-8'))

display(HTML(html))

如何解决此问题?是否有其他解决方法,例如将HTML文件直接嵌入笔记本中?

1 个答案:

答案 0 :(得分:0)

对于Colab笔记本,您应该使用display(HTML(html))

对于Jupyter笔记本,将散点图的HTML写入文件,并使用以下方式将其显示为iframe: IFrame(src=<path of html file>, width = 1300, height=700)