Reputation: 31
When creating an html from a plotly plot using the to_hmtl method, the generated html can be displayed correctly using display(HTML(x)) when using JupyterLab but is not displayed correctly when using Jupyter Notebook (nothing is displayed).
Also, when trying to export the Jupyter Lab notebook to hmtl using nbconvert, the plotly plot are not correctly shown in the generated html (although they are rendered correctly in the JupyterLab Notebook)
Python Code
# Import libraries
import plotly.graph_objs as go
from IPython.display import HTML, display
# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create trace and figure
trace = go.Scatter(x=x, y=y)
fig = go.Figure(data=[trace])
# Generate html figure
html_fig = fig.to_html()
# Display the html figure (Works in JupyterLab. Does not work in Jupyter Notebook)
display(HTML(data=html_fig))
Terminal Code to Convert Notebook to html
jupyter nbconvert --to html PlotlyToHtml.ipynb
Versions
What I tried (without success)
import plotly.io as pio
pio.renderers.default="notebook" # jupyterlab, vscode, ...
html_fig_modified = (
"""<iframe sandbox='allow-scripts allow-forms' style='display:block; margin:0px;' frameborder='0' srcdoc='
<!DOCTYPE html>"""
+ html_fig
+ """ />"""
)
HTML(html_fig_modified)
import holoviews as hv
hv.extension("plotly")
from plotly.offline import init_notebook_mode
init_notebook_mode(connected=True)
Upvotes: 3
Views: 322
Reputation: 1
I'm getting the same behavior. I looked at the plotly code and they seem very aware of this. The .show
method seems to write a temporary html file and then uses an iframe html code which is displayed to visualize the temporary file.
Upvotes: 0