Search code examples
pythonplotlyplotly-python

Saving multiple plots into a single html


I recently discovered plotly and find it really good for graphing, now I have a problem which I want to save multiple plot into a single html, how to do it please?

*I want to save multiple plot, i.e fig, fig1, fig 2 and so on, NOT one subplot which has multiple plot in it, because I found that the plot within subplot is too small.


Solution

  • In the Plotly API there is a function to_html which returns HTML of the figure. Moreover, you can set option param full_html=False which will give you just DIV containing figure.

    You can just write multiple figures to one HTML by appending DIVs containing figures:

    with open('p_graph.html', 'a') as f:
        f.write(fig1.to_html(full_html=False, include_plotlyjs='cdn'))
        f.write(fig2.to_html(full_html=False, include_plotlyjs='cdn'))
        f.write(fig3.to_html(full_html=False, include_plotlyjs='cdn'))
    

    https://plot.ly/python-api-reference/generated/plotly.io.to_html.html

    You can also use Beautiful Soup to do DOM manipulation and insert DIV exactly where you need it in the HTML.

    https://beautiful-soup-4.readthedocs.io/en/latest/#append