Plotly Ipython - display(HTML) 函数无法显示 HTML 文件

5

我正在使用plotly库创建图像并尝试在HTML中显示。我已经将图像格式化为HTML格式。但是display(HTML(report_html))代码无法显示HTML页面。

我正在使用python 3.5和pycharm IDE。我没有使用iPython笔记本。

源代码:

 from IPython.display import display, HTML, Image
 import plotly.plotly as py
 from plotly.offline import init_notebook_mode
 init_notebook_mode()

 data = go.scatter(x=df['date'], y=i, name = long_name['value'])
 figures = [data]
 images = [base64.b64encode(py.image.get(figure, width=width,height=height)).decode('utf-8') for figure in figures]

  report_html = ''
  for image in images:
      _ = template
      _ = _.format(image=image, caption='', width=width, height=height)
      report_html += _

  display(HTML(report_html))

我没有遇到任何错误,只是得到了以下输出:

IPython.core.display.HTML对象


你是在使用Plotly离线模式绘制图像,还是在使用Plotly在线模式?能否澄清一下? - Naren Murali
我正在使用Plotly在线模式。将Plotly图表保存在公共云中。 - Arvinth Kumar
1
我不是专业用户,我可以使用Plotly在线吗? - Naren Murali
是的,有免费用户的社区版。 - Arvinth Kumar
1个回答

2

抱歉回复晚了,这段代码对我来说完美地运行了。让我分享一下我的示例,基本区别是我使用了 plotly.graph_objs 下的 figure对象,而不是 figures = [data]

代码:

from IPython.display import display, HTML, Image
import plotly.plotly as py
import base64
import plotly.graph_objs as go
py.sign_in('<<username here>>', '<<api key here>>')

# required variables
width=500
height=300

# template not provided so created my own
template = """
<div class="row">
    <div class="col-xs-12" style="text-align:center">
        {caption}
    </div>
    <div class="col-xs-12">
        <img src="data:image/png;base64,  {image}" alt="Red dot"/>
    </div>
</div>
"""

# data = go.scatter(x=df['date'], y=i, name = long_name['value'])

# using my sample data instead
trace = go.Bar(x=[2, 4, 6], y= [10, 12, 15])

# layout can also be provided, I am giving as blank
layout = dict()

# figures = [data]
# changing the above commented line to plotly figure object
fig = go.Figure(data=[trace], layout=layout)

# defining list which will contain all the plot objects
figures = []
figures.append(fig)

images = [base64.b64encode(py.image.get(figure, width=width,height=height)).decode('utf-8') for figure in figures]

report_html = ''
for image in images:
    _ = template
    _ = _.format(image=image, caption='', width=width, height=height)
    report_html += _

display(HTML(report_html))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接