I am using python and plotly to product interactive html report. This post gives a nice framework.
If I produce the plot(via plotly) online, and insert the url into the html file, it works but refreshing the charts takes a long time. I wonder if I could produce the chart offline and have it embedded in the html report, so that loading speed is not a problem.
I find plot offline would generate a html for the chart, but I don't know how to embed it in another html. Anyone could help?
I wasn't able to get any of these solutions to work. My goal was to generate plots in one notebook and publish them in another, so persisting the plot HTML wasn't as important for me as simply having some method for serializing the plot to disk to be rebuilt somewhere else.
The solution I came up with is to serialize the
fig
object to JSON, and then use plotly's "json chart schema" to build the plot from JSON. This demo is all python, but it should be trivial to build a plot in HTML using this JSON-serialization strategy and invoking the plotly javascript library directly, if that's what you need.EDIT: Per discussion on the associated github issue, this is probably the current best approach.
There is a better alternative as of right now, that is to do offline plotting into a div, rather than a full html. This solution does not involve any hacks.
If you call:
It creates a file named
file.html
and opens it up in your web browser. However, if you do:the call will return a string with only the div required to create the data, for example:
Notice how its just a tiny portion of an html that you are supposed to embed in a bigger page. For that I use a standard template engine like Jinga2.
With this you can create one html page with several charts arranged the way you want, and even return it as a server response to an ajax call, pretty sweet.
Update:
Remember that you'll need to include the plotly js file for all these charts to work.
You could include
just before putting the div you got. If you put this js at the bottom of the page, the charts won't work.
To improvise the below code you could just call, to_plotly_json() for ex:,
You can also use an iframe according to an answer to the question how to embed html into ipython output?
Option 1: Use plotly's offline functionality in your Jupyter Notebook (I suppose you are using a Jupyter Notebook from the link you are providing). You can simply save the whole notebook as a HTML file. When I do this, the only external reference is to JQuery; plotly.js will be inlined in the HTML source.
Option 2: The best way is probably to code directly against plotly's JavaScript library. Documentation for this can be found here: https://plot.ly/javascript/
Hacky Option 3: If you really want to continue using Python, you can use some hack to extract the HTML it generates. You need some recent version of plotly (I tested it with
plotly.__version__ == '1.9.6'
). Now, you can use an internal function to get the generated HTML:You can simply paste the output somewhere in the body of your HTML document. Just make sure that you include a reference to plotly in the head:
Alternatively, you can also reference the exact plotly version you used to generate the HTML or inline the JavaScript source (which removes any external dependencies; be aware of the legal aspects however).
You end up with some HTML code like this:
Note: The underscore at the beginning of the function's name suggests that
_plot_html
is not meant to be called from external code. So it is likely that this code will break with future versions of plotly.I have recently needed to export Plotly Chart to HTML files.
Here is the simple proper way to do it in 2019.