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

查看:253
本文介绍了以非交互方式运行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核心,我建议看看那些例子表示没有w。

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.

[edit]

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

现在是Jupyter NbConvert。 NbConvert带有一堆默认禁用的预处理器,其中两个( ClearOutputPreprocessor ExecutePreprocessor )是有意义的。你可以通过 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 有方便的 - 执行别名,可以在最新版本的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

例如,在运行笔记本无头后转换为html:

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

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

转换为PDF

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

这(当然)通过产生<$ c与非python内核一起工作$ c>< insert-your-language-here> 内核,如果您设置 - 个人资料=<您的个人资料> 。转换可能非常长,因为它需要重新运行笔记本电脑。您可以使用 - 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-all nbconvert在线文档以获取更多信息。

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天全站免登陆