Displaying HTML file generated by Plotly.offline in a jupyter notebook

Hello!

I’ve been generating some graphs using plotly and I stored them as HTML files to reuse them later.

I now want to display those files in a Jupyter notebook, in the following way:

from IPython.display import HTML

# Not sure if those two lines were necessary, but I tried both with or without them and I obtained the same behaviour
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

HTML('path/to/plotly_file.html)

The code execution completes but the output remains blank afterwards.

Here’s a code that generates a dummy html file (coming from the documentation) :

from IPython.display import HTML, display
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

import numpy as np

N = 1000
random_x = np.random.randn(N)
random_y = np.random.randn(N)

# Create a trace
trace = go.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers'
)

data = [trace]

# Store the figure to a html file
plot(data, filename='basic-scatter.html', auto_open=False)

Has anyone experienced such behaviour? I can display HTML files from other sources, but I can’t seem to understand why Plotly html files aren’t displayed.

Thanks!

Hi @Khreas,

What if you use the filename argument to HTML. See https://github.com/jupyter/notebook/issues/725#issuecomment-297801186.

You might try using an IPython IFrame rather than HTML (See https://ipython.org/ipython-doc/3/api/generated/IPython.display.html#IPython.display.IFrame). For the IFrame case, you might need to place the html file in the same directory and your notebook (or in a subdirectory under the notebook) and use a relative path due to common browser security settings.

Hope that gives you some ideas!

-Jon

1 Like

Hi!

The first solution doesn’t seem to work on my side. However the second one works like a charm in my case, I just need to figure out the width / height of the notebook cells but it should be fine.

Thanks a lot!

-Khreas

1 Like

neither suggestion works for me.
given:

...

# Store the figure to a html file
plot(data, filename='user_data/user_repo/notebooks/basic-scatter.html', auto_open=False)

And verifying the file exists

All of the following HTML commands fail to render the plot

from IPython.display import HTML
HTML(filename='user_data/user_repo/notebooks/basic-scatter.html')
HTML(filename="/mnt/d/Documents/Repos/freqtrade/user_data/user_repo/notebooks/basic-scatter.html")
display(HTML(filename='user_data/user_repo/notebooks/basic-scatter.html'))
display(HTML(filename="/mnt/d/Documents/Repos/freqtrade/user_data/user_repo/notebooks/basic-scatter.html"))

Similarly, the IFrame method yields an IFrame with a 404 error:

from IPython.display import IFrame
IFrame('user_data/user_repo/notebooks/basic-scatter.html', 500,500)
IFrame('/mnt/d/Documents/Repos/freqtrade/absuser_data/user_repo/notebooks/basic-scatter.html', 500,500)
display(IFrame('user_data/user_repo/notebooks/basic-scatter.html', 500,500))
display(IFrame('/mnt/d/Documents/Repos/freqtrade/absuser_data/user_repo/notebooks/basic-scatter.html', 500,500))

My config:

WSL using

PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Renderers configuration
-----------------------
    Default renderer: 'plotly_mimetype+notebook'
    Available renderers:
        ['plotly_mimetype', 'jupyterlab', 'nteract', 'vscode',
         'notebook', 'notebook_connected', 'kaggle', 'azure', 'colab',
         'json', 'png', 'jpeg', 'jpg', 'svg', 'pdf', 'browser',
         'firefox', 'chrome', 'chromium', 'iframe', 'iframe_connected',
         'sphinx_gallery']

$ conda list -n freqtrade
# packages in environment at /mnt/c/users/jravi/miniconda3/envs/freqtrade:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
aiodns                    1.1.1                    pypi_0    pypi
aiohttp                   3.5.4                    pypi_0    pypi
arrow                     0.13.1                   py37_0  
asn1crypto                0.24.0                   py37_0  
async-timeout             3.0.1                    pypi_0    pypi
atomicwrites              1.3.0                    py37_1  
attrs                     19.1.0                   py37_1  
backcall                  0.1.0                    py37_0  
blas                      1.0                         mkl  
ca-certificates           2019.5.15                     0  
cachetools                3.1.1                      py_0    conda-forge
ccxt                      1.18.999                 pypi_0    pypi
certifi                   2019.6.16                py37_1  
cffi                      1.12.3           py37h2e261b9_0  
chardet                   3.0.4                 py37_1003  
click                     7.0                      py37_0  
coinmarketcap             5.0.3                    pypi_0    pypi
coverage                  4.5.3            py37h7b6447c_0  
coveralls                 1.8.2                      py_0  
cryptography              2.7              py37h1ba5d50_0  
cycler                    0.10.0                   py37_0  
cython                    0.29.13                  pypi_0    pypi
dbus                      1.13.6               h746ee38_0  
decorator                 4.4.0                    py37_1  
docopt                    0.6.2                    py37_0  
entrypoints               0.3                      py37_0  
expat                     2.2.6                he6710b0_0  
filelock                  3.0.12                     py_0  
flake8                    3.7.7                    py37_0  
flake8-tidy-imports       2.0.0                    pypi_0    pypi
flake8-type-annotations   0.1.0                    pypi_0    pypi
flask                     1.1.1                      py_0  
fontconfig                2.13.0               h9420a91_0  
freetype                  2.9.1                h8a8886c_1  
freqtrade                 2019.7.dev0               dev_0    <develop>
future                    0.17.1                   py37_0  
glib                      2.56.2               hd408876_0  
gst-plugins-base          1.14.0               hbbd80ab_1  
gstreamer                 1.14.0               hb453b48_1  
icu                       58.2                 h9c2bf20_1  
idna                      2.8                      py37_0  
importlib_metadata        0.17                     py37_1  
intel-openmp              2019.4                      243  
ipykernel                 5.1.1            py37h39e3cac_0  
ipython                   7.7.0            py37h39e3cac_0  
ipython_genutils          0.2.0                    py37_0  
isort                     4.3.21                   py37_0  
itsdangerous              1.1.0                    py37_0  
jedi                      0.13.3                   py37_0  
jinja2                    2.10.1                   py37_0  
joblib                    0.13.2                   py37_0  
jpeg                      9b                   h024ee3a_2  
jsonschema                3.0.1                    py37_0  
jupyter_client            5.3.1                      py_0  
jupyter_core              4.5.0                      py_0  
kiwisolver                1.1.0            py37he6710b0_0  
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 9.1.0                hdf63c60_0  
libgfortran-ng            7.3.0                hdf63c60_0  
libpng                    1.6.37               hbc83047_0  
libsodium                 1.0.16               h1bed415_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
libuuid                   1.0.3                h1bed415_2  
libxcb                    1.13                 h1bed415_1  
libxml2                   2.9.9                hea5a465_1  
markupsafe                1.1.1            py37h7b6447c_0  
matplotlib                3.1.0            py37h5429711_0  
mccabe                    0.6.1                    py37_1  
mkl                       2019.4                      243  
mkl-service               2.0.2            py37h7b6447c_0  
mkl_fft                   1.0.12           py37ha843d7b_0  
mkl_random                1.0.2            py37hd81dba3_0  
more-itertools            7.2.0                    py37_0  
multidict                 4.5.2                    pypi_0    pypi
mypy                      0.701                      py_1  
mypy_extensions           0.4.1                    py37_0  
nbformat                  4.4.0                    py37_0  
ncurses                   6.1                  he6710b0_1  
numpy                     1.16.4           py37h7e9f1db_0  
numpy-base                1.16.4           py37hde5b4d6_0  
openssl                   1.1.1c               h7b6447c_1  
packaging                 19.0                     py37_0  
pandas                    0.25.0           py37he6710b0_0  
parso                     0.5.0                      py_0  
pcre                      8.43                 he6710b0_0  
pexpect                   4.7.0                    py37_0  
pickleshare               0.7.5                    py37_0  
pip                       19.1.1                   py37_0  
plotly                    4.0.0                      py_0  
pluggy                    0.12.0                     py_0  
prompt_toolkit            2.0.9                    py37_0  
psutil                    5.4.8            py37h7b6447c_0  
ptyprocess                0.6.0                    py37_0  
py                        1.8.0                    py37_0  
py-find-1st               1.1.3                    pypi_0    pypi
pycares                   3.0.0                    pypi_0    pypi
pycodestyle               2.5.0                    py37_0  
pycparser                 2.19                     py37_0  
pyflakes                  2.1.1                    py37_0  
pygments                  2.4.2                      py_0  
pyopenssl                 19.0.0                   py37_0  
pyparsing                 2.4.0                      py_0  
pyqt                      5.9.2            py37h05f1152_2  
pyrsistent                0.14.11          py37h7b6447c_0  
pysocks                   1.7.0                    py37_0  
pytest                    5.0.1                    py37_0  
pytest-asyncio            0.10.0                py37_1000  
pytest-cov                2.7.1                      py_0  
pytest-mock               1.10.4                   py37_0  
pytest-random-order       1.0.4                    pypi_0    pypi
python                    3.7.3                h0371630_0  
python-dateutil           2.8.0                    py37_0  
python-dotenv             0.10.3                     py_0    conda-forge
python-rapidjson          0.6.3            py37he6710b0_0  
python-telegram-bot       11.1.0                     py_0    conda-forge
pytz                      2019.1                     py_0  
pyyaml                    5.1.1            py37h7b6447c_0  
pyzmq                     18.0.0           py37he6710b0_0  
qt                        5.9.7                h5867ecd_1  
readline                  7.0                  h7b6447c_5  
requests                  2.22.0                   py37_0  
requests-cache            0.5.0                    pypi_0    pypi
retrying                  1.3.3                    py37_2  
scikit-learn              0.21.2           py37hd81dba3_0  
scikit-optimize           0.5.2                      py_0    conda-forge
scipy                     1.3.0            py37h7c811a0_0  
sdnotify                  0.3.2                    pypi_0    pypi
setuptools                41.0.1                   py37_0  
sip                       4.19.8           py37hf484d3e_0  
six                       1.12.0                   py37_0  
sqlalchemy                1.3.5            py37h7b6447c_0  
sqlite                    3.29.0               h7b6447c_0  
ta-lib                    0.4.17                   pypi_0    pypi
tabulate                  0.8.3                    py37_0  
tk                        8.6.8                hbc83047_0  
tornado                   6.0.3            py37h7b6447c_0  
traitlets                 4.3.2                    py37_0  
typed-ast                 1.3.4            py37h7b6447c_0  
ujson                     1.35             py37h14c3975_0  
urllib3                   1.24.2                   py37_0  
wcwidth                   0.1.7                    py37_0  
werkzeug                  0.15.4                     py_0  
wheel                     0.33.4                   py37_0  
wrapt                     1.11.2           py37h7b6447c_0  
xz                        5.2.4                h14c3975_4  
yaml                      0.1.7                had09818_2  
yapf                      0.27.0                     py_0  
yarl                      1.1.0                    pypi_0    pypi
zeromq                    4.3.1                he6710b0_3  
zipp                      0.5.2                      py_0  
zlib                      1.2.11               h7b6447c_3  

The following code works as an alternative.

import chart_studio
import chart_studio.plotly as py
import chart_studio.tools as tls
import plotly.graph_objects as go

chart_studio.tools.set_credentials_file(username=‘charleschen0611’, api_key=‘wgwS9eStWhbivRK3kZ2z’)

trace = go.Scatter(
x=[1, 2, 3],
y=[4, 5, 6]
)

data = [trace]
layout = go.Layout(title=“My Plot”)

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

Now, instead of using iplot(), you can use py.plot() to upload your plots to your chart_studio account

py.plot(fig, filename = ‘my_plot’, auto_open=True)

display(fig.show())