Reputation:
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
Upvotes: 3
Views: 11748
Reputation: 9800
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.
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.
Upvotes: 6
Reputation: 2283
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
Upvotes: 0