TintinSnowy
TintinSnowy

Reputation: 43

Unable to load Plotly graphs from Jupyter notebook to Github

I have imported following packages in code in my Jupyter notebook and have plotted few graphs. However, when I am uploading python file on github the graphs are not showing. I have also tried using nbviewer as suggested in some of the posts but even in nbviewer the plotly graphs are appearing blank.

Libraries imported:

import pandas as pd

import plotly.express as px

import plotly.graph_objects as go

import plotly.io as pio

from IPython.display import Image

nbviewer URL:

https://nbviewer.org/github/rj180492/Python/blob/main/Covid-19%20Impacts%20Analysis%20using%20Python.ipynb

Upvotes: 2

Views: 2882

Answers (4)

TintinSnowy
TintinSnowy

Reputation: 43

The approach that worked for me was

  • When we save the python file to upload or push to github, we need to save it as .ipynb
  • All the files that I was uploading were .py and so graphs were not rendering, but when I changed the extension the graphs were visible on github.

I am not sure if this is the right solution but after trying all the options and importing different libraries, I could get desired result only after changing extension to .ipynb.

I know this is quite late and might be very trivial but I can finally close the question.

Upvotes: 0

OdyAsh
OdyAsh

Reputation: 1

The method that worked for me was the following: install these libraries:

pip install plotly cufflinks char_studio

then add these lines:

import cufflinks as cf
import chart_studio.plotly as py
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.io as pio
from plotly.offline import init_notebook_mode

then

pio.renderers.default = "notebook_connected"
init_notebook_mode(connected=False)
cf.go_offline()

To be honest, I don't know why that worked, but you can see this as an example

Upvotes: 0

Wayne
Wayne

Reputation: 9790

Those having a related issue should note that the OP wrote in the comments below that despite it working for me via nbviewer when I ran the same notebook and posted my results on GitHub, OP had issues still. What sorted it out in that case so the plots showed up in nbviewer (yet still not in the GitHub quick-preview, which is as expected currently) was going to settings in the repository and choosing 'Pages' > 'Source' and from that dropdown selecting branch as main.



nbviewer is meant to the way to share things with other from the Jupyter ecosystem. (There are some others closely related, such as notebook{sharing}space that is similar yet private by default because you don't need to have it on GitHub and the URLs are generated in a way others won't see unless you share publicly because of complex.) The nice thing about nbviewer is that is is meant for non-tech savvy.

Plotly should render on nbviewer and in many cases is still interactive, such as here where you can click and rotate or zoom the plots, as nbviewer will allow javascript whereas GitHub presently doesn't.
I put in comments that I wasn't seeing what you are and have linked to demonstrations.

Always only think of the GitHub page as a quick preview. See more about this here, here, here, plus there's other present deficiencies I have noted but haven't pointed to other places (text justification with Pandas dataframes, etc.). Static GitHub-notebook viewing in the browser is also not inviting for those unfamiliar with GitHub as it is embedded in an interface meant to get GitHub users to the code & not to feature the notebook content. Nbviewer does a better job featuring the content.

Upvotes: 0

M T
M T

Reputation: 895

Try:

pip install plotly

...as your first cell in the notebook and then try NBviewer. It worked for me. I was pretty sure it was working when it took several minutes for NBviewer to load the first time.

My NBviewer Example

I guess the other libraries are part of Nbviewer by default.

Curiously, my first version just had one plotly plot and it worked. Single color points though. When I got fancy and added another plot that had multiple colors, neither plot rendered. It also took hours to update. After pushing the version with pip install, NBviewer updated right away, it just took a long time to load.

Upvotes: 0

Related Questions