Plotly express doesn't load and refuse to connect

Viewed 387

I have this simple program that should display a pie chart, but whenever I run the program, it opens a page on Chrome and just keeps loading without any display, and sometimes it refuses to connect. How do I solve this?

P.S: I would like to use it offline, and I'm running it using cmd on windows10

import pandas as pd
import numpy as np
from datetime import datetime
import plotly.express as px


def graph(dataframe):
    figure0 = px.pie(dataframe,values=dataframe['POPULATION'],names=dataframe['CONTINENT'])
    figure0.show()


df = pd.DataFrame({'POPULATION':[60,17,9,13,1],'CONTINENT':['Asia','Africa','Europe','Americas','Oceania']})

graph(df)
2 Answers

I'm 99% sure that this is a version issue. It's a long time since you needed an internet connection to build Plotly figures. Follow the instructions here on how to upgrade your system. I've tried your exact code on my end at it produces the following plot:

enter image description here

I'm on Plotly 5.2.2. Run import plotly and plotly.__version__ to check the version on your end.

Disclaimer: I extracted this answer from the OPs question. Answers should not be contained in the question itself.


Answer provided by g_odim_3:

So instead of figure0.show(), I used figure0.write_html('first_figure.html', auto_open=True) and it worked:

import pandas as pd
import numpy as np
from datetime import datetime
import plotly.express as px


def graph(dataframe):
    figure0 = px.pie(dataframe,values=dataframe['POPULATION'],names=dataframe['CONTINENT'],title='Global Population')
#   figure0.show()
    figure0.write_html('first_figure.html', auto_open=True)


df = pd.DataFrame({'POPULATION':[60,17,9,13,1],'CONTINENT':['Asia','Africa','Europe','Americas','Oceania']})

graph(df)
Related