5

I am creating images using plotly library and trying to display in HTML. I have format the images in HTML format. But display(HTML(report_html)) code is not displaying HTML page.

I am using python 3.5 and pycharm IDE. I am not using iPython notebook.

Source code:

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

I am not getting any error. I am just getting following output

IPython.core.display.HTML object

4
  • are you using plotly offline to plot the image, or are you using plotly online mode, can you clarify? Commented Aug 16, 2017 at 7:55
  • I am using plotly online mode. Save plotly charts in public cloud. Commented Aug 16, 2017 at 17:10
  • 1
    I am not a pro user can I use plotly online? Commented Aug 16, 2017 at 17:17
  • Yes. There is a community edition for free users Commented Aug 16, 2017 at 18:24

1 Answer 1

2

Sorry for the late reply, The code works perfectly for me, let me share my sample,the basic difference is I used the figure object under plotly.graph_objs instead of figures = [data]

Code:

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.