With GitHub we can store our code online, and with Jupyter notebook we can execute only a segment of our Python code. I want to use them together. I am able to edit code wit
Github is a tool for version and source control, you will need to get a copy of the code to a local environment.
There is a beginner's tutorial here
Once that you have set up a github account and define your local and remote repositories, you will be able to retrieve the code with git checkout
. Further explanation is in the tutorial
The IPython Magic command %load
, as described in tip# 8 here, will replace the contents of the Jupyter notebook cell with an external script.
The source can either be a file on your computer or a URL.
The trick with a Github or Gist-hosted script is to direct it at the URL for raw code. You can get the URL for the raw code by browsing the script on GitHub or gist.github.com and pressing Raw
in the toolbar just above the code. Place what you extract from the address bar after %load
to get something along the lines of this:
%load https://raw.githubusercontent.com/dib-lab/khmer/master/scripts/fastq-to-fasta.py
That will pull in the code to the notebook's namespace when you execute it in a Jupyter notebook cell.
More about using raw code via GitHub or Gists here and here. More on other magic commands can be found here.
Similarly, if you want to bring the script in as a file you can call in the notebook using %run
(or from the command line equivalent), use curl
in the notebook cell and the script will be added to the current directory.
!curl -O https://raw.githubusercontent.com/dib-lab/khmer/master/scripts/fastq-to-fasta.py
Or if you want others to be able to easily run that notebook.
Check out MyBinder.org highlighted in this Nature article here. More information on the service can be found here, here, and here.
At the MyBinder.org page you can point the service at any Github repository. The caveat though is that unless it is fairly vanilla python in the notebook, you'll hit dependency issues. You can set it up to address that as guided by here and here.
That was done to produce this launchable repo after I forked one that had not been set up to use the Binder system initially. Another example, this one R code, based on a gist shared on a twitter exchange can be seen here.
Using that, you can get a Launch Binder
badge that you can add to your repository and launch it any time. See an example that you can launch here.