Unable to load Plotly graphs from Jupyter notebook to Github
Asked Answered
T

4

2

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

Twinberry answered 31/5, 2022 at 14:5 Comment(7)
This is not easy to troubleshoot in the form you currently shared it because it isn't a minimal reproducible example. Your data is in transformed_data.csv and a similar file not included. At least some plotly express and go..Figure() plots should be working in nbviewer, as you can see see here. I'd like to check yours if you provide data.Beneficence
Thanks, the csv file is at location- github.com/rj180492/PythonTwinberry
I'm not seeing what you see. I used the iris dataset and converted a couple examples of your code to use that. Then I saved the notebook and uploaded it to Github. As you can see here nbviewer, fully renders the plots. Think of Githyub as just a preview. The don't allow javascript to render which is necessary for this to work as it does via nbviewer. Notebook itself is here.Beneficence
Maybe you had something set to not share on Plotly when you rendered, because you can see by the raw code, yours looks very similar, with things like document.getElementById('9c9bc569-093b-4b76-8b09-b494fedc1148'). So it should work. I'll try running your data next.Beneficence
Still cannot run yours because you only provide one of the data. And I cannot simply replace data2 = pd.read_csv("./transformed_data.csv").Beneficence
Sorry, added both files nowTwinberry
With you data, here on nbviewer. Notebook on Github at here. It seems to work. I am rendering it from binder sessions launched here where I don't sign into plotly.Beneficence
B
0

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.

Beneficence answered 31/5, 2022 at 16:21 Comment(4)
Thanks for the information and help. But still the graphs are not appearing for me in nbviewer. nbviewer.org/github/rj180492/Python/blob/main/…Twinberry
However, can you confirm you see them in the nbviewer renderings of the links I made? If so, I suggest you do the same thing I did to run your notebook yet again and try with a version of the notebook saved from there. I suspect your Plotly settings are set to not be public and you aren't enabling it. Running it in the way I did seems to bypass that or by default they are public.Beneficence
Finally, my nbviewer started showing graphs. nbviewer.org/github/rj180492/Python/blob/main/… One change that I did was for the repository went to Settings-->Pages-->Source and from dropdown selected branch as main.Then clicked Save. Got this update- Your site is published at rj180492.github.io/Python And now nbviewer works!Twinberry
@Beneficence do you know if there are limits to the python packages available in NBviewer by default? See my answer to this question.Pulling
P
0

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.

Pulling answered 20/6, 2022 at 8:49 Comment(14)
About it taking hours to show the current updates.... see the last two paragraphs at the bottom of my post here about how you can bypass waiting when you need to check.Beneficence
About "I guess the other libraries are part of Nbviewer by default." ... I don't believe that is true. The content is all static. It's just that some packages have output that is based around javascript and storage of associated content online that allow static pages in which that content is embedded continue to work interactively with the rendering engine and templates built into nbviewer.Beneficence
@wayne thanks. I'm sure I didn't say it right but I still don't understand how that would explain my last sentence. Once I included the pip install, nbviewer updated immediately and both plots displayed. I had seen your workaround article already and while that did seem to help with the update, it did not help the display problem. Also, I'm not real clear on how plotly works "under the hood" but it is not my impression that my plot is stored on some server somewhere. I run plotly in standalone mode (again, maybe not the right term). I have no plotly account. I may try some more experiments.Pulling
Oh sorry, I must have read it fast and thought you were talking about how nbviewer doesn't display the updated version of a page for a while. It sounds like your plotly plots just took a long time to load? If they had a ton of data or where your data went in the plotly system and nbviewer are far apart that would be expected. Or possible communication between nbviewer and plotly was slow that day?Beneficence
@wayne. Hehe... still not quite right. Take a look at my link. I ended up at this question because it wasn't updating and went through all the suggestions. THEN I added "pip install plotly" and it worked... after taking a long time the first time. Now it is quick. So I concluded the pip helped in some way. Maybe, maybe not! Seems like you are implying that plotly plots ALWAYS communicate with some external system (plotly), which I was not aware of. Interesting though, which is why I'm diving in to details. Thanks for your comments!Pulling
Hmmm...it seems you didn't have plotly installed to where the environment was backing the notebook if executing pip install plotly in your notebook (which actually runs the equivalent of %pip install plotly, see here) caused it to work. The extra time the first time was mostly it installing plotly and everything it needs, I'd guess.Beneficence
The source of where I sent you for examples here installs plotly in the environment via pip because it is listed here. Press launch binder there to get active notebooks in session like I used to save the versions there. That way I know for sure what is installed. You should have gotten an error when trying to import plotly though if it wasn't installed. Maybe it needed updating and that triggered it. I'm guessing here. It is weird.Beneficence
Quick FYI that may totally not be pertinent, but I saw it....This suggestion may be helpful to keep in mind in case there is some current issue that it helps.Beneficence
@wayne I guess you're still missing that this is not a local machine Jupyter Lab problem, it's an NBViewer by way of github problem. The packages are not part of the .ipynb file I uploaded to NBViewer so it must be up to NBViewer to have them available (somewhere) for import. I am new to displaying nb's on github or NBViewer and don't really understand how it works, but unless my logic is way off, it has to be an NBV "thing." Everything works fine on my local machine... it was only once trying to show it on the web that I had to get tricky. Does that make sense to you maybe?Pulling
Try the "download" button on the github repo to see what NBViewer really gets passed.Pulling
I've lost the train of what you are trying to say. nbviewer doesn't care if your notebook has pip install plotly in it or not. (You should be able to demonstrate it yourself by running the notebook with it there and then deleting that cell and saving the notebook.) nbviewer doesn't read that line and do anything with it. nbviewer cares if the notebook was executed and saved from an environment where plotly was installed and working so that among the json text that is passed it is javascript embedded in it that uses javascript code and CSS to render the plot on a 'static' web page.Beneficence
(Oh wait, you are suggesting nbviewer saw your addition of pip install plotly and reacted to load plotly?) You can see the javascript functions and CSS what is shown from the 'Download' button following lines like <script type=\"text/javascript\">\n", " window.PlotlyConfig = {MathJaxConfig: 'local'};\n",. The plotly.js has been added into the .ipynb file and nbviewer is happy to use it. nbviewer isn't reading the pip install code in the json representation of the notebook file.Beneficence
When I suggested, "You should be able to demonstrate it yourself by running the notebook with it there and then deleting that cell and saving the notebook". That "and then deleting that cell and saving the notebook" part meant to delete the cell with pip install plotly after you run the notebook and then save the freshly executed notebook, now lacking the pip install plotly cell, to your machine and and add to GitHub. It should render just fine at nbviewer by using the plotly.js embedded in the .ipynb file that got saved when it was run.Beneficence
NOW you got it, thanks! And yes, I found the PlotlyConfig so I just learned a lot. The previous "nbviewer updated when I installed plotly" must have been a coincindence. And even pasting the raw link doesn't always seem to work, but it does seem to help.Pulling
S
0

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

Sheugh answered 21/12, 2022 at 7:22 Comment(0)
T
0

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.

Twinberry answered 17/6 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.