How to save code file on GitHub and run on Jupyter notebook?

一曲冷凌霜 提交于 2020-03-16 06:20:48

问题


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


回答1:


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 easily get that URL by browsing the script on GitHub and pressing Raw in the toolbar just above the code. Combine what you extract from the address bar 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

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.




回答2:


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



来源:https://stackoverflow.com/questions/40054672/how-to-save-code-file-on-github-and-run-on-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!