Fork me on GitHub

Jupyter and JupyterLab: Sharing notebooks

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • How can I share notebooks with colleagues and the community?
Objectives
  • See what platforms and services exist to share Jupyter notebooks.
  • Have a final discussion on notebooks in research.

Sharing notebooks

  • You can enter a URL, GitHub repo or username, or GIST ID in nbviewer and view a rendered Jupyter notebook
  • Read the Docs can render Jupyter Notebooks via the nbsphinx package
  • Binder creates live notebooks based on a GitHub repository
  • EGI Notebooks (see also https://egi-notebooks.readthedocs.io)
  • JupyterLab supports sharing and collaborative editing of notebooks via Google Drive
  • Notedown, Jupinx and DocOnce can take Markdown or Sphinx files and generate Jupyter Notebooks
  • Voilà allows you to convert a Jupyter Notebook into an interactive dashboard
  • The jupyter nbconvert tool can convert a (.ipynb) notebook file to:
    • python code (.py file)
    • an HTML file
    • a LaTeX file
    • a PDF file
    • a slide-show in the browser

Commercial offers with free plans

These platforms can be used free of charge but have paid subscriptions for faster access to cloud resources:


Exercise (20 min): Making your notebooks reproducible by anyone via Binder

  • Create a GitHub repository.
  • Push the notebook which we have created earlier to this repository. If you got stuck earlier, you can fork this repository: https://github.com/coderefinery/jupyter (our example notebook is under example)
  • Create a requirements.txt file which contains:
    matplotlib==3.1.0
    
  • Commit and push also this file to your notebook repository.
  • Visit https://mybinder.org:
  • Check that your notebook repository now has a “launch binder” badge in your README.md file on GitHub.
  • Try clicking the button and see how your repository is launched on Binder (can take a minute or two). Your notebooks can now be expored and executed in the cloud.
  • Enjoy being fully reproducible!

(Optional) Exercise: what happens without requirements.txt?

Let’s look at the same activity inequality repository. We can start this repository in Binder by using this link.

  • Start the repository in Binder
  • fig3/fig3bc.ipynb is a Python notebook, so works in Binder. Most others are in R, which also works in Binder. But how? Try to run the notebook - can you make it work?
  • Install the missing requirements with pip. Does it work now? Why or why not?
  • How would this be better?

(Optional) Exercise: share an interactive (ipywidgets) notebook via Binder

(Optional) Exercise: share R Markdown/R Studio project via Binder

This exercise is for those who use Rmd files instead of Jupyter notebooks.

  • Put your Rmd file into a GitHub repository.
  • To this repository add a file runtime.txt which specifies the R version you want to use:
    r-3.6-2020-10-13
    
  • To this repository add a file install.R which lists the dependencies, for instance:
    install.packages(c("readr", "ggplot2"))
    
  • After you have done that, visit https://mybinder.org/v2/gh/myuser/myrepo/mybranch?urlpath=rstudio (adapt “myuser”, “myrepo”, and “mybranch”).
  • For more information, see this guide.

Recommendations for longer notebooks

Create a table of contents on top

You can do that using Markdown. This produces a nice overview for longer notebooks. Example: https://stackoverflow.com/a/39817243

How to make it possible to toggle showing code

It is possible to hide all the code and only show the output. This can be nice for notebook readers who don’t need/want to see the code:

from IPython.display import HTML

HTML('''<script>
code_show=true;
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')

Final discussion

  • If you are already using Jupyter, what tasks do you use it for?
  • If you are new to Jupyter, do you see any possible use cases?
  • Do you think Jupyter Notebooks can help tackle the problem of irreproducible results?