3

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 with Jupyter notebook that is stored on my computer. But, I am unable to find a way to run a code that stored on GitHub. So, do you know a way to do that.

Here are some examples: https://github.com/biolab/ipynb/blob/master/2015-bi/lcs.ipynb https://github.com/julienr/ipynb_playground/blob/master/misc_ml/curse_dimensionality.ipynb https://github.com/rvuduc/cse6040-ipynbs/blob/master/01--intro-py.ipynb

2 Answers2

6

1. If you just want to run Python code hosted on Github or in a Gist:

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

Then you'd run the script in your Jupyter notebook with the following in a cell:

%run fastq-to-fasta.py

Provide any necessary arguments for the script after its name. Often you can run %run <script_name> --help to get information on what arguments the script expects. Using %run in the notebook provides a more full-featured Jupyter experience; the use of %run to run a script in a Jupyter notebook is similar to the traditional way to run a script on the command line.

2. If you want to run a notebook placed on GitHub:

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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • I should have added that you can make your efforts more stable/reproducible by you and others if you reference a specific commit at Github when using the `%load` command. You can browse in the history of commits and then enter specific ones and `Browse files` via the GitHub web interface and get the raw links to a specific commit. Building on the example above, you'd have `%load https://raw.githubusercontent.com/dib-lab/khmer/2c87d813c2e39550ece844db10a9d55b4dadc209/scripts/fastq-to-fasta.py` to specify the version of the script committed September 14, 2017. – Wayne Feb 05 '18 at 20:50
0

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

Carlos Monroy Nieblas
  • 2,225
  • 2
  • 16
  • 27
  • Thanks for answering. –  Oct 15 '16 at 03:42
  • But, in the examples above, it seems people are storing the code on GitHub and running on Jupyter notebook. Just have a look: https://github.com/rvuduc –  Oct 15 '16 at 03:45
  • you'll need to go through the tutorial to define your local repository and check out the branch that you are interested to run – Carlos Monroy Nieblas Oct 15 '16 at 03:55