Output of plotly to_html() method is not displayed in Jupyter Notebook

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 plotly.graph_objs as go
from IPython.display import HTML, display

# Create some sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# Create a trace
trace = go.Scatter(
    x=x,
    y=y,
    mode='markers',
    marker=dict(
        size=10,
        color='blue'
    )
)

# Create a data list with the trace
data = [trace]

# Create a layout
layout = go.Layout(
    title='Example Scatter Plot',
    xaxis=dict(title='X-axis'),
    yaxis=dict(title='Y-axis')
)

# Create a figure with the data and layout
fig = go.Figure(data=data, layout=layout)

html_fig = fig.to_html()

display(HTML(html_fig))

For further information you can also see the original post on StackOverflow https://stackoverflow.com/q/77317461/21157254

Yes you right. the html version of the plot isn’t displayed. I’m wondering why anyone would want to convert to html a Plotly figure generated in the Jupyter Notebook, and then display it again in Jupyter notebook, while fig.show() could do this job. I have been using Plotly for 10 years and have never, absolutely never needed to perform such a β€œtrick”.

Thank you for confirming the problem. I am generating a standard html report using a jinja2 template and I am embedding the plotly plots as html in the report. Then I sometime need to render this report (or several) inside a Jupyter Notebook to document my work. I guess it is kind of a non standard solution but in my case fig.show() will not do the trick. :face_with_diagonal_mouth:
Any ideas how to solve the issue?