python - Plotly Ipython - 显示(HTML)函数不显示 html 文件

标签 python ipython plotly

我正在使用 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 object

最佳答案

很抱歉回复晚了,代码对我来说很完美,让我分享我的示例,基本区别是我使用了 plotly.graph_objs 下的 figure 对象 而不是 figures = [数据]

代码:

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))

关于python - Plotly Ipython - 显示(HTML)函数不显示 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45704166/

相关文章:

node.js - 如何在 Plotly 中显示所有 x 轴刻度值?

python - 使用 Fig.update_layout Plotly 更新 Traces 的可见性

python - 在同一图中绘制 2 个或更多函数

python - 如何在Python中从两个列表创建字典

python - numpy.tile 不能作为 Matlab repmat

python - 在 ipython 中使用 timeit 的问题

python-3.x - 如何摆脱 Python Pandas 中的斜体并获取纯文本?

python - 在python中读取一个pgm文件

python - Bokeh jupyter 中的圆形图未显示

python - 单击条形图中的条形可生成该条形图中值的散点图