Overview
Teaching: 10 min
Exercises: 0 minQuestionsObjectives
- How can I share notebooks with colleagues and the community?
- See what platforms and services exist to share Jupyter notebooks.
- Have a final discussion on notebooks in research.
nbviewer
and view a rendered Jupyter notebookjupyter nbconvert
tool can convert a (.ipynb
) notebook file to:
.py
file)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
- Take one of these two notebooks:
- Push it to a GitHub/GitLab repository (you can also add both files to the same repository).
- Create a
requirements.txt
file in your notebook repository, e.g.:ipywidgets==7.4.2 numpy==1.16.4 matplotlib==3.1.0
- Try to deploy this example via Binder in the same way as the above exercise.
(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.
You can do that using Markdown. This produces a nice overview for longer notebooks. Example: https://stackoverflow.com/a/39817243
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>''')