Search code examples
pythonplotlyplotly-python

(python plotly 5.7.0) It doesn't display graph when ther's NO INTERNET, it still required internet to show graphs


I'm using python 3.6.8 and plotly 5.7.0

[user1@testsrv1 ~]$ python3 --version
Python 3.6.8
[user1@testsrv1 ~]$ pip3 freeze | grep -i plotly
plotly==5.7.0
[user1@testsrv1 ~]$ pip3 show plotly
Name: plotly
Version: 5.7.0
Summary: An open-source, interactive data visualization library for Python
Home-page: https://plotly.com/python/
Author: Chris P
Author-email: [email protected]
License: MIT
Location: /home/opc/.local/lib/python3.6/site-packages
Requires: tenacity, six

I'm expecting the plotly graphs to be displayed in server where there's no internet connection, but it's NOT displaying. As per plotly document, from plotly 4, graphs are Offline only -- https://medium.com/plotly/plotly-py-4-0-is-here-offline-only-express-first-displayable-anywhere-fc444e5659ee! I'm using below code snippet:

import pandas as pd
import plotly.express as px
graph1 = px.histogram(dataframe, x='TIME_BUCKET', color='CATEGORY',  title='Category Histogram')
graph2 = px.bar(dataframe, x="CATEGORY", y="COUNT", title="Category wise execution count")
                with open("report.html", 'w') as f:
                    f.write(graph1.to_html(full_html=False, include_plotlyjs='cdn'))
                    f.write(graph2.to_html(full_html=False, include_plotlyjs='cdn'))

Now when I open report.html (from VM whare there's No internet connection), it doesn't show any graph.

But when I forcefully use plotly offline like below the html shows graph.

plotly.offline.plot(graph1, filename=outhtml_path, auto_open=False)

I'm expecting plotly normal functions (not the pyhon.offline.plot) to show the graphs in VMs without any internet connection.


Solution

  • I got the solution. in below code f.write(graph1.to_html(full_html=False, include_plotlyjs='cdn')) f.write(graph2.to_html(full_html=False, include_plotlyjs='cdn')) changed include_plotlyjs='cdn' to include_plotlyjs=True and it solved the problem.

    f.write(graph1.to_html(full_html=False, include_plotlyjs=True))
    f.write(graph2.to_html(full_html=False, include_plotlyjs=True))
    

    As per documentation (https://plotly.github.io/plotly.py-docs/generated/plotly.html),

    include_plotlyjs (bool or string (default True)) – Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code (~3MB) is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If ‘cdn’, a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library.