'How publish R code in html using notebook jupyter

I see many people talk about converting and sharing codes to HTML using jupyter notebooks like it is a piece of cake but it has turned into a nightmare for me!

I can sure convert my codes into html by downloading them using the dropdown menu but it is only accessible on my own machine and if I send it as a link to someone else, they will see :this site cant be reached". The HTML download only gives a link on local host and it is not shareable. Does anyone know what is the way around it? I did use github but that seems only for Jupyter Python and not R does.

Is there any help here?



Solution 1:[1]

Way at the end you mention using Github. You can share the notebook directly in it's .ipynb form (actually json encoded) using that or Github's Gist site and anyone you send a link can see it as nicely rendered static. You can of course publish to html, but then you need to host html or use githack to link to it. There is always the option of going to PDF and sending that via email. Since you talk about converting I assume you don't need it to be active; however, that is possible too using MyBinder.org, see an example here where you can click the top 'launch' button and spin up a notebook someone else previously made.

I'll step through each of those suggestions with further clarification.


notebook{sharing}space

notebook{sharing}space billed as "the fastest way to share your notebooks", it works with both R markdown and Jupyter notebooks. Allows annotations and highlights directly on the notebook using hypothes.is and can be private if you limit sharing the link.

GitHub and nbviewer to share a notebook

You can use Github to share a notebook if it is public. Imagine you made a public repo and put a notebook there in its .ipynb file form. (Note: all those steps can be done directly in Github's web interface without you needing git on your local computer.) Let's use here as a theoretical example. If you go to that repo you'll see a notebook index.ipynb listed. Click on the link to directly view it. (You should now be here.) The notebook may render or it may try and timeout. Github's rendering isn't perfect so don't rely on it. Better is to use the nbviewer site. And to use the nbviewer site, you'll want to copy the URL of the notebook from your address bar. For this example, you'd copy:

https://github.com/binder-examples/r/blob/master/index.ipynb

With that URL in your clipboard, go to the nbviewer site at https://nbviewer.jupyter.org/ . When you get to nbviewer's front page, paste the URL in the form at the top under 'Enter the location of a Jupyter Notebook...' and click Go!. That will take you to a nice page of the notebook rendered without any Github header/cruft. Note that it doesn't matter if it is Python code or R code in the notebook. It will look nice at nbviewer. To share this view with someone copy the URL from the address bar of this page. In this case it will be:

https://nbviewer.jupyter.org/github/binder-examples/r/blob/master/index.ipynb

You can share that link with anyone now.

That link tells the nbviewer site to fetch the notebook code from Github and render it. If you update it later, they can still use the same link and get the updated form opening. (Note that nbviewer can sometimes take 10 or so minutes before it collects updates from Github.) It is possible to also share a specific version of a notebook using Github's URLS and nbviewer. You'd want to learn about how browse the files of specific commits at Github to do that and use those URLs.

Something to note is that the link for the nbviewer rendering page follows a pattern referring to the Github repo, and so it is possible to make the link without going through all those steps.

Another thing to note is that there is a symbol with three rings in the upper right corner, just to the left of the download button. If you click on those rings you can launch an active version of that notebook via MyBinder.org. When the session spins up, that notebook will run without need for installing any additional packages or anything if the repository was configured so the underlying tech has recognized the configuration files and built the proper environment. It would still open the notebook if the repository wasn't set up with Binder in mind, but you may get errors when you try to run as the environment isn't completely compatible by default.

Gist site and nbviewer to share a notebook

This process is very similar to the one described under 'GitHub and nbviewer to share a notebook'. The first few steps about where you put the notebook .ipynb file differ. You'd go to Github's Gist site at https://gist.github.com/ . This site is meant for sharing snippets of code or just a few files and doesn't have all the features a full Github repository has. However, it is great for your uses. You can paste the contents of the .ipynb file or upload it there and then use the URL over at nbviewer as described for described under 'GitHub and nbviewer to share a notebook'.

Gist site and githack to share a notebook's HTML

This process is very similar to the one described under 'Gist site and nbviewer to share a notebook'. However, what you upload to the Gist site and the tech you use to share a rendering differ. This time you'll want to put the HTML file you mentioned at the Gist site. It will look like HTML code and not a notebook at this point. Copy the URL of the page at the Gist site to your clipboard. Go to the raw.githack.com site at http://raw.githack.com/ . Paste the URL in the form line at the top where it says to paste a URL. You'll now go to a page that is the rendering of the HTML. You can copy the URL from your address bar and share that with anyone, similar to how you use nbviewer to view a notebook.

Github repository and githack to share a notebook's HTML

This is very similar to what is described under 'Gist site and githack to share a notebook's HTML'. The difference is you'd use an actual Github repo to store the HTMl version of the notebook.

Share a PDF of the notebook

You didn't mention this route. So I'll keep it brief. Depending on your installation of Jupyter or where you are using it, I've seen saving a PDF form be inconsistent. One place it should be possible, is here. If you go there and press the launch binder button, you can spin up a session where PDF saving works. You'll want to specifically use the Export to PDF icon that will appear on the too menubar just above the notebook.

Share an active form of your notebook

I'll also keep this brief since it was touched on in various forms in the sections above and is more advanced than using nbviewer.

One route to doing this was described at the end of the 'GitHub and nbviewer to share a notebook' section. You can do this without using nbviewer as well. Let's go back to the example repo here. Copy the URL form the top of that page:

https://github.com/binder-examples/r

Now go to the MyBinder.org site at https://mybinder.org/ and paste that URL into the top of the form where it says 'GitHub repository name or URL' and click launch. This will spin up a temporary session using the environment configured by special files it has seen in that repository. You'll be able to click on the notebook index.ipynb and run it. Never share the running session link as it is temporary.

To get something to share so that others can launch there own session with your notebook, you'll want to explore using the options for form more at https://mybinder.org/ to generate links you can share with others. For example, you can add in a path to a specific notebook so when the session launches it starts with that notebook already opened. Once you have filled in the form you want to copy for sharing the URL that you'll see generated under 'Copy the URL below and share your Binder with others:'. You'll see that form also allows making code for clickable badges like you see at https://github.com/binder-examples/r .

Note the launching of the session with the notebook that can be run works because that repository was configured to install the irkernel. It won't work to run just any notebook from any repository with R without some configuration. (It will default to only having Python kernels.) However, the example repository I have been referencing can be used as a template to make a new repository of your own using the Use this template button here to ease the steps needed to do this. There is also Holepunch to help in similar steps for sharing R-based code.

Combining tech when you want to share a private notebook .ipynb file and let someone view or run it

You could also send people the notebook .ipynb file and then they have some options. They can use nbpreview which allows you to choose a local file to view in your browser. The notebook is only viewable via this tool. The notebook file is actually only on your local machine in your browser's local cache and so it is good for sharing sensitive notebooks and provide a suggestion for people who don't commonly use notebooks render it as it should be. If it is super sensitive work in the notebook and folks involved aren't trusting because nbpreview is an online site even though what you point it at remains local only, it is also possible to install and run nbpreview locally, see here. It is also possible to install nbviewer locally and view it. Or they can run the notebook file you sent using their Jupyter system. Because elements of some of those options relies advanced skills and knowing how to install things, they may not be best for all involved.

One way to circumvent those hurdles is to set up a repository capable of serving via MyBinder an environment fully suited for running the private notebook and then advise the user how to spin up a session and upload the notebook that you shared with them separately, such as via email. You can see here for more on security on MyBinder.org.

Wait... there's more

As notebooks become more popular, there becomes more ways to use and share them. This answer only touched on some of the more open, public ways. When you add in Jupyter Hubs with authorization there are still more because you can share a link to your own notebook in the JupyterHub network. Also there are commercial offerings now. You could always use Google Colab or Azure notebooks to share them as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1