标签归档:nbconvert

如何从终端运行.ipynb Jupyter Notebook?

问题:如何从终端运行.ipynb Jupyter Notebook?

我在.ipynb文件中有一些代码,并且到了我真的不需要IPython Notebook的“交互”功能的地步。我想直接从Mac Terminal命令行运行它。

基本上,如果这只是一个.py文件,我相信我可以从命令行执行python filename.py。.ipynb文件是否存在类似内容?

I have some code in a .ipynb file and got it to the point where I don’t really need the “interactive” feature of IPython Notebook. I would like to just run it straight from a Mac Terminal Command Line.

Basically, if this were just a .py file, I believe I could just do python filename.py from the command line. Is there something similar for a .ipynb file?


回答 0

在命令行中,您可以使用以下命令将笔记本转换为python:

jupyter nbconvert --to python nb.ipynb

https://github.com/jupyter/nbconvert

您可能需要安装python mistune软件包:

sudo pip install -U mistune

From the command line you can convert a notebook to python with this command:

jupyter nbconvert --to python nb.ipynb

https://github.com/jupyter/nbconvert

You may have to install the python mistune package:

sudo pip install -U mistune

回答 1

nbconvert允许您运行带有--execute标志的笔记本:

jupyter nbconvert --execute <notebook>

如果您想运行一个笔记本并生产一个新的笔记本,则可以添加--to notebook

jupyter nbconvert --execute --to notebook <notebook>

或者,如果您想用新的输出替换现有的笔记本:

jupyter nbconvert --execute --to notebook --inplace <notebook>

由于这是一个很长的命令,因此可以使用别名:

alias nbx="jupyter nbconvert --execute --to notebook"
nbx [--inplace] <notebook>

nbconvert allows you to run notebooks with the --execute flag:

jupyter nbconvert --execute <notebook>

If you want to run a notebook and produce a new notebook, you can add --to notebook:

jupyter nbconvert --execute --to notebook <notebook>

Or if you want to replace the existing notebook with the new output:

jupyter nbconvert --execute --to notebook --inplace <notebook>

Since that’s a really long command, you can use an alias:

alias nbx="jupyter nbconvert --execute --to notebook"
nbx [--inplace] <notebook>

回答 2

您可以从中导出所有代码.ipynb并将其另存为.py脚本。然后,您可以在终端中运行脚本。

希望能帮助到你。

You can export all your code from .ipynb and save it as a .py script. Then you can run the script in your terminal.

Hope it helps.


回答 3

在终端中运行ipython:

ipython

然后找到您的脚本并放在此处:

%run your_script.ipynb

In your Terminal run ipython:

ipython

then locate your script and put there:

%run your_script.ipynb

回答 4

对于新版本,而不是:

ipython nbconvert --to python <YourNotebook>.ipynb

您可以使用ipython的jupyter实例:

jupyter nbconvert --to python <YourNotebook>.ipynb

For new version instead of:

ipython nbconvert --to python <YourNotebook>.ipynb

You can use jupyter instend of ipython:

jupyter nbconvert --to python <YourNotebook>.ipynb

回答 5

更新作者引用的评论,以提高可见度:

作者注释:“该项目在Jupyter的执行API之前启动,现在是从命令行运行笔记本的推荐方式。请考虑弃用不维护runipy的软件。” –塞巴斯蒂安·帕尔马(Sebastian Palma)

安装允许在终端上运行代码的runipy库

pip install runipy

只是编译您的代码后:

runipy <YourNotebookName>.ipynb

您也可以尝试cronjob。所有信息都在这里

Update with quoted comment by author for better visibility:

Author’s note “This project started before Jupyter’s execute API, which is now the recommended way to run notebooks from the command-line. Consider runipy deprecated and unmaintained.” – Sebastian Palma

Install runipy library that allows running your code on terminal

pip install runipy

After just compiler your code:

runipy <YourNotebookName>.ipynb

You can try cronjob as well. All information is here


回答 6

就我而言,最适合我的命令是:

jupyter nbconvert --execute --clear-output <notebook>.ipynb

为什么?此命令不会创建额外的文件(就像.py文件一样),并且每次执行Notebook时,单元的输出都会被覆盖。

如果您运行:

jupyter nbconvert --help

–clear-output清除当前文件的输出并保存到位,覆盖现有笔记本。

In my case, the command that best suited me was:

jupyter nbconvert --execute --clear-output <notebook>.ipynb

Why? This command does not create extra files (just like a .py file) and the output of the cells is overwritten everytime the notebook is executed.

If you run:

jupyter nbconvert --help

–clear-output Clear output of current file and save in place, overwriting the existing notebook.


回答 7

您还可以使用该boar软件包在python代码中运行笔记本。

from boar.running import run_notebook

outputs = run_notebook("nb.ipynb")

如果更新笔记本,则无需再次将其转换为python文件。


有关更多信息,请访问:

https://github.com/alexandreCameron/boar/blob/master/USAGE.md

You can also use the boar package to run your notebook within a python code.

from boar.running import run_notebook

outputs = run_notebook("nb.ipynb")

If you update your notebook, you won’t have to convert it again to a python file.


More information at:

https://github.com/alexandreCameron/boar/blob/master/USAGE.md


回答 8

从终端运行

jupyter nbconvert --execute --to notebook --inplace --allow-errors --ExecutePreprocessor.timeout=-1 my_nb.ipynb

默认超时为30秒。-1消除了限制。

如果您希望将输出笔记本保存到新笔记本中,则可以使用该标志 --output my_new_nb.ipynb

From the terminal run

jupyter nbconvert --execute --to notebook --inplace --allow-errors --ExecutePreprocessor.timeout=-1 my_nb.ipynb

The default timeout is 30 seconds. -1 removes the restriction.

If you wish to save the output notebook to a new notebook you can use the flag --output my_new_nb.ipynb


回答 9

我有同样的问题,我找到了造纸厂。与其他解决方案相比的优势在于,您可以在笔记本计算机运行时看到结果。当笔记本电脑花费很长时间时,我发现此功能很有趣。这是非常容易使用:

pip install papermill
papermill notebook.ipynb output.ipynb

它还具有其他方便的选项,例如将输出文件保存到Amazon S3,Google Cloud等。有关更多信息,请参阅页面。

I had the same problem and I found papermill. The advantages against the others solutions is that you can see the results while the notebook is running. I find this feature interesting when the notebook takes very long. It is very easy to use:

pip install papermill
papermill notebook.ipynb output.ipynb

It has also, other handy options as saving the output file to Amazon S3, Google Cloud, etc. See the page for more information.