Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly not loading in browser

I know there is a similar thread over here, but I have not managed to find any solutions within.

When I try to run some of the example Plotly code, the page never seems to load for me.

For instance, I run the below in VSCode

import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.show()

Every instance of me running opens the localhost with a different port, and the page never successfully loads.

e.g. http://127.0.0.1:56840/ , http://127.0.0.1:56887/

It’s only when I write to html, with the below code, does it appear as intended.

import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.write_html('first_figure.html', auto_open=True)

Is there anyway to use fig.show() as described in the documentation?

Hi @etjkai, and thanks for using the community forum!

What web browser are you trying to use? Do you have any extensions which prevent JavaScript from executing?

Do you have the same issues displaying figures if you try to run a Jupyter Notebook in that same browser?

I would also recommend taking a look at https://plotly.com/python/renderers/. Maybe your default renderer is not set to browser?

Hi Joseph, thanks for trying to help me!

I’m using Chrome. I do not have any extensions that prevent JavaScript. (write_html method works just perfectly)

There’s no trouble running it in Jupyter Notebook (I use the Python Interactive Jupyter Server, in VS Code).
My pio.renderers.default return “browser”

The last time I worked with plotly, I don’t recall it having the 5 digit port number at the end of the localhost, is it supposed to be there?

EDIT: I have also turned off my firewall temporarily, to no avail.

As far as seeing the port number is concerned, yes, that is normal. plotly.py uses a port to communicate the information about your figure to the browser.

Does your issue only occur in Chrome or does it persist in other web browsers? For example, if you change your default browser to Firefox and then run fig.show(), are you able to view your figure?

Okay I’ve tried, and embarrassingly it works with firefox, and on chrome on another computer.

How can I troubleshoot what is going wrong with Chrome on this computer? AFAIK I do not have any add-ons / extensions that stop JavaScript - It’s the same chrome account on two computers.

Javascript is enabled on my Chrome browser - https://i.imgur.com/4guu6Md.png

Not sure if it’s pertinent info, but it doesn’t always not work. Sometimes, it loads well for two tries, then stops during future runs.