Convert Plotly objects to HTML or Images

I am curious whether we can render plotly html from plotly objects.

So I have an object <plotting_file.PlotFunctions object at 0x7f57f763bf98>, here plotting_file is my file name and PlotFunctions is my class name. In one of the methods under this class say method1(), I return an object of plotly like this,

plot_func = go.Figure(data=data, layout=layout)

html_plot = py.plot(plot_func, filename='./static/plotted_file.html', auto_open=False)
return html_plot

Now I am rendering this html_plot in flask but unfortunately I cannot. When I zeroed down little bit, I found that html_plot is this value <plotting_file.PlotFunctions object at 0x7f57f763bf98> but now the file.

Kindly guide me to attain this. I need that object to be rendered as a HTML file.

Thanks in advance.

Hi @acesaif,

when you call plotly.offline.plot with output_type='file' (which is the default) then the function will write out a file, and it won’t return the html contents as a string. If you want a string version of the HTML you have two options

  1. Open the file after it’s written out with something like:
html_plot = py.plot(plot_func, filename='./static/plotted_file.html', auto_open=False)
with open('./static/plotted_file.html', 'r') as f:
    html =
return html
  1. Ask plotly.offline.plot to return an html <div> element as a string with something like:
html_div = py.plot(plot_func, output_type='div')
return html_div

In this case you would need to insert the div in a full HTML page somewhere else.

Also, if you haven’t already considered it, I’d recommend taking a look at Dash ( as a way to build flask apps with plotly visualizations using only Python.

Hope that helps!

1 Like

I figured it out. Thanks a lot.

1 Like