Apparently, it turned out that upgrading plotly version solved the problem. To upgrade plotly, I simply run the following codes to check the plotly version.
import plotly
plotly.__version__
That gave me the output of version of plotly as '5.11.0'. Then I upgraded the plotly version to '5.11.0' by writing the following code:
pip install plotly==5.11.0
Answer from S. Mandal on Stack Overflowpython - Why am I getting error message as "ModuleNotFoundError: No module named 'plotly.express" while it was working before? - Stack Overflow
Plotly Express cannot open a local website page
python - How to save plotly express plot into a html or static image file? - Stack Overflow
Why am I getting error message as "ModuleNotFoundError: No module named 'plotly.express" while it was working before?
Videos
Apparently, it turned out that upgrading plotly version solved the problem. To upgrade plotly, I simply run the following codes to check the plotly version.
import plotly
plotly.__version__
That gave me the output of version of plotly as '5.11.0'. Then I upgraded the plotly version to '5.11.0' by writing the following code:
pip install plotly==5.11.0
I had the same issue, and it was because had the python file named as "plotly.py" which triggered the error:
ModuleNotFoundError: No module enamed 'plotly.express'; 'plotly' is not a package
Renaming the file fixes the issue.
I am using python 3.9 and IDLE. I am trying to plot a simple graph and to visualize it on my Chrome browser. Below are two implementations. The first implementation works fine and it displays the graph interactively on a local web page.
The second implementation opens a web page that points to an IP adress http://127.0.0.1:59932/ and does not display the graph. After a while it comes with a message: This site canโt be reached.
Can someone explain why the second implementation goes to the http://127.0.0.1:59932/ address and not to a temorary local address of the plot that the library could create. Please suggest how to fix it. Thanks.
First implementation:
import plotly.express as px
fig =px.scatter(x=range(10), y=range(10))
fig.write_html("C:/Users/Jim/example.html", auto_open = True)Second implementation:
import plotly.express as px fig =px.scatter(x=range(10), y=range(10)) fig.show()
Updated answer:
With newer versions of plotly, static Image export in Python is a breeze. Just make sure to install kaleido using:
pip install -U kaleido
or, for Anaconda:
conda install -c conda-forge python-kaleido
And then run
fig.write_image("yourfile.png")
Filetypes such as .jpeg and .pdf are also available options.
Producing an individual html file is still very easy:
Just use plotly.offline.plot(fig, filename='C:/plotlyplots/canada_offline.html')
This will give you a html file of a plotly express bar chart with the name lifeExp in a desired folder. Remember import plotly and not only import plotly.express as px.
Complete code:
# imports
import plotly
import plotly.express as px
# data
df = px.data.gapminder().query("continent=='Oceania'")
# plotly express bar chart
fig = px.line(df, x="year", y="lifeExp", color='country')
# html file
plotly.offline.plot(fig, filename='C:/plotlyplots/lifeExp.html')
Plot:

File as it appears in the foler:

From here you can open the file in any browser or any other way you want.
Here's the content as it is displayed using Notepad++

If you don't mind a bit of manual labor, you dan save a .png version using the toolbar:

Old answer for static images:
Producing a static image automatically is a bit mote tricky.
Take a look at Static Image Export in Python if you prefer that to html.
I like to use the approach including orca that can produce a variety of image files. I haven't found any other way to install it other than using npm which is installed with node.js If you get that in order, you only have to go run the following to get it up and running (I'm on Windows):
npm install -g [email protected] orca
pip install psutil requests
Then you can change the last line in the snippet above with fig.write_image("C:/plotlyplots/lifeExp.png") to produce a .png file.
Adding to @vestland 's answer about saving to HTML, another way to do it according to the documentation would be:
import plotly.express as px
# a sample scatter plot figure created
fig = px.scatter(x=range(10), y=range(10))
fig.write_html("path/to/file.html")
You can read about it further (controlling size of the HTML file) here: Interactive HTML Export in Python