1

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

  • 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](https://nbviewer.org/github/fomightez/3Dscatter_plot-binder/blob/master/Plotly3d-scatter-plots.ipynb). I'd like to check yours if you provide data. – Wayne May 31 '22 at 15:23
  • Thanks, the csv file is at location- https://github.com/rj180492/Python – TintinSnowy May 31 '22 at 15:45
  • 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](https://nbviewer.org/gist/fomightez/3ecb0891f66b523fd908af6e7cebbf53), 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](https://gist.github.com/fomightez/3ecb0891f66b523fd908af6e7cebbf53). – Wayne May 31 '22 at 15:45
  • 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. – Wayne May 31 '22 at 15:47
  • Still cannot run yours because you only provide one of the data. And I cannot simply replace `data2 = pd.read_csv("./transformed_data.csv")`. – Wayne May 31 '22 at 15:51
  • Sorry, added both files now – TintinSnowy May 31 '22 at 15:53
  • With you data, [here on nbviewer](https://nbviewer.org/urls/gist.githubusercontent.com/fomightez/3ecb0891f66b523fd908af6e7cebbf53/raw/cf2d9b63187127c7fa5195e626599d525c64260f/Covid-19%2520Impacts%2520Analysis%2520using%2520Python.ipynb). Notebook on Github at [here](https://gist.github.com/fomightez/3ecb0891f66b523fd908af6e7cebbf53#file-covid-19-impacts-analysis-using-python-ipynb). It seems to work. I am rendering it from binder sessions launched [here](https://github.com/fomightez/3Dscatter_plot-binder) where I don't sign into plotly. – Wayne May 31 '22 at 16:00

3 Answers3

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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • Thanks for the information and help. But still the graphs are not appearing for me in nbviewer. https://nbviewer.org/github/rj180492/Python/blob/main/Covid-19%20Impacts%20Analysis%20using%20Python.ipynb?flush_cache=True – TintinSnowy Jun 01 '22 at 15:45
  • 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. – Wayne Jun 01 '22 at 18:11
  • 1
    Finally, my nbviewer started showing graphs. https://nbviewer.org/github/rj180492/Python/blob/main/Covid-19%20Impacts%20Analysis%20using%20Python.ipynb 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 https://rj180492.github.io/Python/ And now nbviewer works! – TintinSnowy Jun 02 '22 at 09:58
  • @Wayne do you know if there are limits to the python packages available in NBviewer by default? See my answer to this question. – M T Jun 20 '22 at 08:50
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.

M T
  • 863
  • 1
  • 7
  • 15
  • About it taking hours to show the current updates.... see the last two paragraphs at the bottom of [my post here](https://discourse.jupyter.org/t/markdown-code-snippets-not-rendering-after-uploading-notebook-to-github/14480/2?u=fomightez) about how you can bypass waiting when you need to check. – Wayne Jun 20 '22 at 15:05
  • 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. – Wayne Jun 20 '22 at 15:09
  • @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. – M T Jun 22 '22 at 21:05
  • 1
    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? – Wayne Jun 22 '22 at 21:15
  • @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! – M T Jun 22 '22 at 21:43
  • 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](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez)) caused it to work. The extra time the first time was mostly it installing plotly and everything it needs, I'd guess. – Wayne Jun 23 '22 at 00:55
  • The source of where I sent you for examples [here](https://github.com/fomightez/3Dscatter_plot-binder) installs plotly in the environment via pip because it is listed [here](https://github.com/fomightez/3Dscatter_plot-binder/blob/master/requirements.txt). 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. – Wayne Jun 23 '22 at 01:01
  • Quick FYI that may totally not be pertinent, but I saw it....[This suggestion](https://stackoverflow.com/a/72743479/8508004) may be helpful to keep in mind in case there is some current issue that it helps. – Wayne Jun 24 '22 at 15:53
  • @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? – M T Jun 30 '22 at 04:21
  • Try the "download" button on the github repo to see what NBViewer really gets passed. – M T Jun 30 '22 at 04:23
  • 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. – Wayne Jun 30 '22 at 17:29
  • 1
    (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 ` – Wayne Jun 30 '22 at 17:40
  • 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. – Wayne Jun 30 '22 at 17:47
  • 1
    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. – M T Jun 30 '22 at 18:04
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

OdyAsh
  • 1
  • 1