0

I have been able to embed this map in an IPython Notebook (which is sweet), but I am not clear on how I can share this with folks not using the Notebook. I am familiar with the bl.ocks.org viewer. It's great for standalone examples, but I am looking to share the rest of the analysis in the Notebook along with interactive charts. Neither the HTML conversion of the Notebook nor the nbviewer rendering can locate the map (I get a 404 message).

After the first 404 (with this gist), I changed the viewer function to capture the github location of the map file (V2). I am not yet clear why, but that change stopped nbviewer from even rendering the surrounding materials. Any thoughts on a better way to go about this?

Marvin Ward Jr
  • 1,019
  • 1
  • 11
  • 30
  • Your link are wrong. Please update them. (Or you made your gist private, in which case you need to make them public.) – Matt Dec 29 '13 at 11:24
  • Sorry about that, didn't realize a shortened version had been transferred. The gist links should work now. – Marvin Ward Jr Dec 29 '13 at 15:21
  • No problem. **Do not** edit the ipynb files directly. Your change (`+filename+`) made the file not valid json. Nbviewer **is not** running your code what you save in your notebook will be what will be shown, there is no point in trying to have nbviewer interprete some variable name. Also `from IPyhton.display import IFrame` exists and avoid issues of manually using `HTML(')` – Matt Dec 29 '13 at 19:23

1 Answers1

1

The trouble is that the map is saved as a local HTML file (rChart_map.html) and is hence not accessible to nbviewer when you are trying to view it online.

Even if you upload rChart_map.html to the gist, it won't show up due to path issues. Locally, you need to refer to it as /files/rChart_map.html in your IPython notebook, whereas online, it has a different path. I had posted this issue earlier on twitter using the #IPython tag, but got no responses on how to debug.

So where does that leave us. Well, fortunately, most modern browsers allow an iframe to contain inline HTML using the srcdoc tag. This allows the generated .ipynb file to be standalone, as seen here, at the end of the file.

The key is to use the following code. The first line creates an iframe with inline html of the map and stores it in the python variable map2. The second line imports the necessary python modules and the third line displays the HTML. Note that we use h2[0], since map2 is an array, due to conversion from R, which is vectorized.

map2 = %R paste(capture.output(map$show('iframesrc', cdn = TRUE)), collapse = '\n')
from IPython.display import display, HTML
HTML(map2[0])

For this to work, you will need to have rCharts version > 0.4.1.

I am interested in making it easier for rCharts to be used in IPython notebooks. So any suggestions/feedback is welcome.

Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • This is a great workaround that will certainly be useful for me. I am not sure that I have sufficient HTML knowledge to provide the most efficient development advice (most of my work with HTML are small hack-y type interventions). I can provide use cases that perhaps have some overlap with others. I'll drop them into your [Issues](https://github.com/ramnathv/rCharts/issues) stream as they come up, unless there is a preferred alternative. Thank you for your help. I am once again grateful for the speed and effectiveness of your response. – Marvin Ward Jr Dec 29 '13 at 17:07
  • Do not refer to local files with `/files/` but `files/` no leading slash. it will work on nbviewer if you include the linked files next to your ipynb. – Matt Dec 29 '13 at 19:18
  • Agreed there might be a bug in nbviewer that prevent file from working with gists (need to add a trailing slash to url) – Matt Dec 29 '13 at 19:26
  • So, will `files/file.html` work both locally and online with nbviewer? – Ramnath Dec 29 '13 at 20:53
  • @Matt Adding a trailing slash also does not work. Can you post an example showing how I can make a `files/...` link work in `nbviewer`? – Ramnath Dec 29 '13 at 21:01
  • Apparently it does not work with gist (bug was probably introduces when we migrated to tornado not so long ago) issues 115 oppened on nbviewer for fix. `files/` should work on local and nbviewer, and should not even be necessary with IPython master anymore. – Matt Dec 30 '13 at 10:18