如何将代码文件保存在GitHub上并在Jupyter Notebook上运行? [英] How to save code file on GitHub and run on Jupyter notebook?

查看:2642
本文介绍了如何将代码文件保存在GitHub上并在Jupyter Notebook上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GitHub,我们可以在线存储代码,而使用Jupyter笔记本,我们只能执行Python代码的一部分.我想一起使用.我可以使用存储在计算机上的Jupyter笔记本编辑代码.但是,我无法找到一种方法来运行存储在GitHub上的代码.所以,您知道一种方法吗?

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.

以下是一些示例: https://github.com/biolab/ipynb/blob/master /2015-bi/lcs.ipynb https://github.com/julienr/ipynb_playground/blob/master/misc_ml /curse_Dimensionity.ipynb https://github.com/rvuduc/cse6040- ipynbs/blob/master/01--intro-py.ipynb

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.如果您只想运行Github或Gist中托管的Python代码:

IPython Magic命令%load,如 tip中所述#8这里,将用外部脚本替换Jupyter笔记本单元的内容.

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.

源可以是您计算机上的文件,也可以是URL.
Github或Gist托管脚本的技巧是将其指向原始代码的URL.您可以通过在GitHub上浏览脚本并在代码上方的工具栏中按Raw轻松获取该URL.结合从地址栏中提取的内容,可以大致理解以下内容:

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

当您在Jupyter笔记本单元中执行代码时,会将代码拉入笔记本的命名空间.
有关通过GitHub或Gists使用原始代码的更多信息,此处此处 .有关其他魔术命令的更多信息,请参见

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.

类似地,如果要将脚本作为文件导入,则可以使用%run(或等效于命令行)在笔记本中调用,在笔记本单元格中使用curl,脚本将被添加到当前目录.

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.如果要运行放置在GitHub上的笔记本:

或者如果您希望其他人能够轻松运行该笔记本.
查看此《自然》文章 MyBinder.org
. article/d41586-018-01322-9"rel =" nofollow noreferrer>此处.有关该服务的更多信息,请此处此处

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.

在MyBinder.org页面上,您可以将服务指向任何Github存储库.但要注意的是,除非笔记本中的Python非常原始,否则您会遇到依赖关系问题.您可以按照此处此可启动仓库.最初是Binder系统.另一个示例是基于在Twitter交流上共享的要旨的一个R代码 此处.

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.

使用它,您可以获取一个Launch Binder徽章,可以将其添加到存储库中并随时启动它.看到一个可以在此处启动的示例.

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.

这篇关于如何将代码文件保存在GitHub上并在Jupyter Notebook上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆