以非交互方式运行 IPython/Jupyter 笔记本 [英] Running an IPython/Jupyter notebook non-interactively

查看:31
本文介绍了以非交互方式运行 IPython/Jupyter 笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否可以从命令行以非交互方式运行 IPython/Jupyter 笔记本,并将生成的 .ipynb 文件与运行结果一起保存.如果还不可能,那么用 phantomJS 实现、打开和关闭内核以及打开和关闭 Web 服务器会有多难?

Does anyone know if it is possible to run an IPython/Jupyter notebook non-interactively from the command line and have the resulting .ipynb file saved with the results of the run. If it isn't already possible, how hard would it be to implement with phantomJS, something to turn the kernel on and off, and something to turn the web server on and off?

更具体地说,假设我已经有一个笔记本 original.ipynb,我想重新运行该笔记本中的所有单元格并将结果保存在一个新的笔记本中 new.ipynb,但在命令行上使用一个命令执行此操作,无需在浏览器中进行交互或关闭内核或 Web 服务器,并且假设没有内核或 Web 服务器已在运行.

To be more specific, let's assume I already have a notebook original.ipynb and I want to rerun all cells in that notebook and save the results in a new notebook new.ipynb, but do this with one single command on the command line without requiring interaction either in the browser or to close the kernel or web server, and assuming no kernel or web server is already running.

示例命令:

$ ipython notebook run original.ipynb --output=new.ipynb

推荐答案

是的,这是可能的,而且很容易,它将(主要)在 2.0 的 IPython 核心中,我建议查看 那些例子现在.

Yes it is possible, and easy, it will (mostly) be in IPython core for 2.0, I would suggest looking at those examples for now.

$ jupyter nbconvert --to notebook --execute original.ipynb --output=new.ipynb

它现在在 Jupyter NbConvert 中.NbConvert 带有一堆默认禁用的 Preprocessor,其中两个(ClearOutputPreprocessorExecutePreprocessor)很有趣.您可以通过 c.<PreprocessorName>.enabled=True(大写的 python)在您的(本地|全局)配置文件中启用它们,或者在命令行上使用 --ExecutePreprocessor.enabled=True 照常保留命令的其余部分.

It is now in Jupyter NbConvert. NbConvert comes with a bunch of Preprocessors that are disabled by default, two of them (ClearOutputPreprocessor and ExecutePreprocessor) are of interest. You can either enabled them in your (local|global) config file(s) via c.<PreprocessorName>.enabled=True (Uppercase that's python), or on the command line with --ExecutePreprocessor.enabled=True keep the rest of the command as usual.

--ExecutePreprocessor.enabled=True 具有方便的 --execute 别名,可用于最新版本的 NbConvert.如果需要,它可以与 --inplace 结合使用

The --ExecutePreprocessor.enabled=True has convenient --execute alias that can be used on recent version of NbConvert. It can be combine with --inplace if desired

例如在notebook headless运行后转成html:

For example, convert to html after running the notebook headless :

$ jupyter nbconvert --to=html --execute RunMe.ipynb

剥离输出后转换为 PDF

converting to PDF after stripping outputs

$ ipython nbconvert --to=pdf --ClearOutputPreprocessor.enabled=True RunMe.ipynb

如果您设置 --profile=<your最喜欢的个人资料>.转换可能真的很长,因为它需要重新运行笔记本.您可以使用 --to=notebook 选项进行笔记本到笔记本的转换.

This (of course) does work with non-python kernels by spawning a <insert-your-language-here> kernel, if you set --profile=<your fav profile>. The conversion can be really long as it needs to rerun the notebook. You can do notebook to notebook conversion with the --to=notebook option.

根据用例,可能需要设置/取消设置各种其他选项(超时、允许错误等).查看文档,当然还有 jupyter nbconvert --help--help-allnbconvert 在线文档 了解更多信息.

There are various other options (timeout, allow errors, ...) that might need to be set/unset depending on use case. See documentation and of course jupyter nbconvert --help, --help-all, or nbconvert online documentation for more information.

这篇关于以非交互方式运行 IPython/Jupyter 笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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