重用来自不同 IPython 笔记本的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16966280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reusing code from different IPython notebooks
提问by user7289
I am using IPython and want to run functions from one notebook from another (without cutting and pasting them between different notebooks). Is this possible and reasonably easy to do?
我正在使用 IPython 并希望从另一个笔记本运行功能(无需在不同笔记本之间剪切和粘贴它们)。这可能并且相当容易做到吗?
采纳答案by Mike Müller
You can connect with a qtconsole to the same kernel. Just supply this at startup:
您可以使用 qtconsole 连接到同一个内核。只需在启动时提供:
ipython qtconsole --existing kernel-0300435c-3d07-4bb6-abda-8952e663ddb7.json
Look at the output after starting the notebook for the long string.
在启动笔记本后查看长字符串的输出。
回答by Matt
Starting your notebook server with:
使用以下命令启动笔记本服务器:
ipython notebook --script
will save the notebooks (.ipynb) as Python scripts (.py) as well, and you will be able to import them.
还将笔记本 ( .ipynb)保存为 Python 脚本 ( .py),您将能够导入它们。
Or have a look at: http://nbviewer.ipython.org/5491090/that contains 2 notebook, one executing the other.
或者看看:http: //nbviewer.ipython.org/5491090/包含 2 个笔记本,一个执行另一个。
回答by Roland
So @MikeMuller's good idea will work for a local notebook, but not a remote one (right?). I don't think there is a way for you to remotely invoke individual cell blocks or functions of ipynb code on a remote server and be able to get results back into your calling routine programmatically, unless that code does something fairly extraordinary to communicate results.
所以@MikeMuller 的好主意适用于本地笔记本,但不适用于远程笔记本(对吧?)。我不认为有一种方法可以让您远程调用远程服务器上的 ipynb 代码的单个单元块或函数,并且能够以编程方式将结果返回到您的调用例程中,除非该代码做了一些非常特别的事情来传达结果。
I was in the process of writing when @Matt submitted the same idea about
当@Matt 提交相同的想法时,我正在写作
ipython <URI_to_Notebook> --script
ipython <URI_to_Notebook> --script
The *.pynb is a JSON container and not an actual python script. You can get ipython to export a *.py with
*.pynb 是一个 JSON 容器,而不是实际的 python 脚本。你可以让 ipython 导出一个 *.py
If the target *.ipynb is on a remote machine you don't control, you'll probably need to pull the file so that you can write the output to a local path. (Haven't looked into whether you can invoke this on a remote resource to create a local output.) Once this is created you should be able to import and run the *.py or individual functions within it.
如果目标 *.ipynb 位于您无法控制的远程机器上,您可能需要拉取文件,以便将输出写入本地路径。(还没有研究是否可以在远程资源上调用它来创建本地输出。)创建后,您应该能够导入和运行 *.py 或其中的单个函数。
A question for @Matt on that neat example of running another *.ipynb file wholesale with io.open(nbfile)is whether the nbfile can be remote? Seems like a long shot, but would be great...
@Matt 关于运行另一个 *.ipynb 文件批发的简洁示例的一个问题是 nbfileio.open(nbfile)是否可以远程?看起来很远,但会很棒......
回答by drevicko
Ipythons %runmagic allows you execute python files and ipython scripts in a notebook. I sometimes use the -ioption so it runs in the notebooks namespace. Execute a cell with %run?in it for more info.
Ipythons%run魔法允许你在笔记本中执行 python 文件和 ipython 脚本。我有时会使用该-i选项,因此它会在 notebooks 命名空间中运行。执行一个单元格以%run?获取更多信息。
You can use the ipython --scriptto save notebooks also as .pyfiles on each save or uncomment the line c.NotebookManager.save_script=Truein your ipython_notebook_config.pyfile for the same effect (use ipython profile createfor setting that up - on Ubuntu the config files live in ~/.config/ipython/).
您可以使用ipython --script保存的笔记本电脑也为.py在每次保存或取消对该行的文件c.NotebookManager.save_script=True在你的ipython_notebook_config.py文件相同的效果(使用ipython profile create用于设置了- Ubuntu的文件都在config ~/.config/ipython/)。
Edit:The following is true, but unnecessary - you can %runa .ipynbfile directly. Thanks Eric.
编辑:以下是正确的,但没有必要 - 您可以直接%run创建.ipynb文件。谢谢埃里克。
If you use ipython magics in the notebook you want to import, I found that you can rename the .pyfile to .ipy(an ipython script), but I had to remove the first line (which contained the file encoding declaration) for it to work. There is probably a better way! This approach will likely confuse cell magics too (they'd all get applied at once).
如果您在要导入的笔记本中使用 ipython 魔法,我发现您可以将.py文件重命名为.ipy(ipython 脚本),但我必须删除第一行(其中包含文件编码声明)才能使其工作。可能有更好的方法!这种方法也可能会混淆细胞魔法(它们都会立即应用)。
回答by Eric Drechsel
In IPython 2.0 you can simply %run 'my_shared_code.ipynb'to share code between notebooks. See for example http://nbviewer.ipython.org/gist/edrex/9044756.
在 IPython 2.0 中,您可以简单%run 'my_shared_code.ipynb'地在笔记本之间共享代码。参见例如http://nbviewer.ipython.org/gist/edrex/9044756。
回答by atiasnir
Here are two additional tips:
这里有两个额外的提示:
You can also run
%qtconsolemagic directly from the notebook and it will automatically connect to the notebook kernel.Check out https://github.com/atiasnir/ipnb
You can use it to import notebook files as if they're standard python modules (I'm the author :-)). Main limitation here is that it will discard magic cells (because it does not use IPython at all) but otherwise it should work fine.
您也可以
%qtconsole直接从 notebook运行magic,它会自动连接到 notebook 内核。查看https://github.com/atiasnir/ipnb
您可以使用它来导入笔记本文件,就好像它们是标准的 Python 模块一样(我是作者 :-))。这里的主要限制是它会丢弃魔法单元(因为它根本不使用 IPython),否则它应该可以正常工作。
回答by Jan Katins
There is also a "write and execute" extension, which will let you write the content of a cell to a file (and replace old content -> update code), which can then be imported in another notebook.
还有一个“写入和执行”扩展,它可以让您将单元格的内容写入文件(并替换旧内容 -> 更新代码),然后可以将其导入到另一个笔记本中。
https://github.com/minrk/ipython_extensions#write-and-execute
https://github.com/minrk/ipython_extensions#write-and-execute
In one notebook (two cells)
在一个笔记本中(两个单元格)
%reload_ext writeandexecute
--
%%writeandexecute -i some_unique_string functions.py
def do_something(txt):
print(txt)
And then in the other notebook:
然后在另一个笔记本中:
from functions import do_something
do_something("hello world")
回答by volodymyr
I do call notebooks from other notebooks. You can even pass "parameters" to other notebooks using the following trick:
我确实从其他笔记本中调用笔记本。您甚至可以使用以下技巧将“参数”传递给其他笔记本:
Place paramsdictionary in the first cellof "report_template.ipynb".
将params字典放在“report_template.ipynb”的第一个单元格中。
params = dict(platform='iOS',
start_date='2016-05-01',
retention=7)
df = get_data(params ..)
do_analysis(params ..)
And in another (higher logical level) notebook, execute it using this function:
在另一个(更高的逻辑级别)笔记本中,使用以下函数执行它:
def run_notebook(nbfile, **kwargs):
"""
example:
run_notebook('report.ipynb', platform='google_play', start_date='2016-06-10')
"""
def read_notebook(nbfile):
if not nbfile.endswith('.ipynb'):
nbfile += '.ipynb'
with io.open(nbfile) as f:
nb = nbformat.read(f, as_version=4)
return nb
ip = get_ipython()
gl = ip.ns_table['user_global']
gl['params'] = None
arguments_in_original_state = True
for cell in read_notebook(nbfile).cells:
if cell.cell_type != 'code':
continue
ip.run_cell(cell.source)
if arguments_in_original_state and type(gl['params']) == dict:
gl['params'].update(kwargs)
arguments_in_original_state = False
run_notebook("report_template.ipynb", start_date='2016-09-01')
This command will execute each cell of the "report_template" notebook and will override relevant keys of paramsdictionary starting from the second cell
此命令将执行“report_template”笔记本的每个单元格,并从第二个单元格开始覆盖params字典的相关键
回答by michael
Yes, you can "run functions from one notebook from another (without cutting and pasting them between different notebooks)"-- and, yes, it's easy to do!
是的,您可以“从一个笔记本运行另一个笔记本中的功能(无需在不同笔记本之间剪切和粘贴它们)”——而且,是的,这很容易做到!
tl;dr:put the code in python files (*.py) in the file system & let multiple notebooks use the same code. (It's that simple.)
tl; dr:将代码放在*.py文件系统中的 python 文件 ( ) 中,让多个笔记本使用相同的代码。(就是这么简单。)
(Why put so much code in notebooks, when we have perfectly good code editors & IDEs that are so much better for writing & reading code? Not to mention the need for proper version control! What are we trying to achieve, and at what expense? </rant>)
(当我们有非常好的代码编辑器和 IDE 可以更好地编写和阅读代码时,为什么要在笔记本中放置这么多代码?更不用说需要适当的版本控制了!我们要实现什么目标,付出的代价是什么? ? </rant>)
Details:
细节:
- Put your code in normal python files, eg
my_code/foo.py, adding a (probably empty)my_code/__init__.py - Take advantage of having the code under proper version control (eg git) -- notice how hard it was to diff ipynb json files?
- Also put the notebooks also under version control. Raw git logs will be hard to read, but the comments can be useful. (GitHub/GitLab displays ipython notebooks, btw.)
- Limit the py source in the ipynb notebook to small amounts of "driver" code, and output, and documentation.
- See also: https://ipython.org/ipython-doc/stable/config/extensions/autoreload.html
- If you want to "inline" the external python files, just use (for example) magic
%cat my_code/foo.py...
- 将您的代码放在普通的 python 文件中,例如
my_code/foo.py,添加一个(可能是空的)my_code/__init__.py - 利用代码在适当的版本控制下(例如 git)——注意区分 ipynb json 文件有多难?
- 还将笔记本也置于版本控制之下。原始 git 日志将难以阅读,但注释可能很有用。(GitHub/GitLab 显示 ipython 笔记本,顺便说一句。)
- 将 ipynb 笔记本中的 py 源限制为少量的“驱动程序”代码、输出和文档。
- 另见:https: //ipython.org/ipython-doc/stable/config/extensions/autoreload.html
- 如果您想“内联”外部 python 文件,只需使用(例如)magic
%cat my_code/foo.py...
...If you want something fancier to display that source inline (optionally, adding the following to an external, reusable source file)...
...如果您想要更高级的内联显示该源代码(可选地,将以下内容添加到外部的、可重用的源文件中)...
import IPython
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_for_filename
filename='my_code/foo.py'
with open(filename) as f: code = f.read()
formatter = HtmlFormatter(linenos='inline')
IPython.display.HTML('<style type="text/css">{}</style>{}'.format(
formatter.get_style_defs('.highlight'),
highlight(code, get_lexer_for_filename(filename), formatter)))
Your favorite code editors & IDEs thank you for your support.
您最喜欢的代码编辑器和 IDE,感谢您的支持。
回答by Mattijn
I use the following function in the notebook from which I want to load functions or actions from a source notebook:
我在要从源笔记本加载功能或操作的笔记本中使用以下函数:
import io
import nbformat
def execute_notebook(nbfile):
with io.open(nbfile, encoding="utf8") as f:
nb = nbformat.read(f, as_version=4)
ip = get_ipython()
for cell in nb.cells:
if cell.cell_type != 'code':
continue
ip.run_cell(cell.source)
Use like:
像这样使用:
execute_notebook(path/to/notebook.ipynb)

