Python 如何从终端运行 .ipynb Jupyter Notebook?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35545402/
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
How to run an .ipynb Jupyter Notebook from terminal?
提问by Vincent
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.
我在 .ipynb 文件中有一些代码,并且已经达到了我并不真正需要 IPython Notebook 的“交互式”功能的程度。我想直接从 Mac 终端命令行运行它。
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?
基本上,如果这只是一个 .py 文件,我相信我可以从命令行执行 python filename.py 。.ipynb 文件有类似的东西吗?
采纳答案by ditkin
From the command line you can convert a notebook to python with this command:
从命令行,您可以使用以下命令将笔记本转换为 python:
jupyter nbconvert --to python nb.ipynb
https://github.com/jupyter/nbconvert
https://github.com/jupyter/nbconvert
You may have to install the python mistunepackage:
您可能需要安装 python mistune包:
sudo pip install -U mistune
回答by pdm
回答by minrk
nbconvert allows you to run notebooks with the --execute
flag:
nbconvert 允许您使用以下--execute
标志运行笔记本:
jupyter nbconvert --execute <notebook>
If you want to run a notebook and produce a new notebook, you can add --to notebook
:
如果你想运行一个笔记本并生成一个新的笔记本,你可以添加--to notebook
:
jupyter nbconvert --execute --to notebook <notebook>
Or if you want to replacethe 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>
回答by Axis
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
作者的注释“这个项目在 Jupyter 的执行 API 之前开始,现在是从命令行运行笔记本的推荐方式。考虑 runipy 已弃用和未维护。” – 塞巴斯蒂安·帕尔马
Install runipy library that allows running your code on terminal
安装 runipy 库,允许在终端上运行您的代码
pip install runipy
After just compiler your code:
在编译你的代码之后:
runipy <YourNotebookName>.ipynb
You can try cronjob as well. All information is here
您也可以尝试 cronjob。所有信息都在这里
回答by Vijay Panchal
For new version instead of:
对于新版本而不是:
ipython nbconvert --to python <YourNotebook>.ipynb
You can use jupyter instend of ipython:
您可以使用 ipython 的 jupyter instend:
jupyter nbconvert --to python <YourNotebook>.ipynb
回答by alejandro
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.
为什么?此命令不会创建额外的文件(就像.py
文件一样),并且每次执行笔记本时都会覆盖单元格的输出。
If you run:
如果你运行:
jupyter nbconvert --help
--clear-output Clear output of current file and save in place, overwriting the existing notebook.
--clear-output 清除当前文件的输出并保存到位,覆盖现有笔记本。
回答by Martin Ptacek
In your Terminal run ipython:
在您的终端中运行 ipython:
ipython
then locate your script and put there:
然后找到您的脚本并将其放在那里:
%run your_script.ipynb
回答by Alex
You can also use the boar
package to run your notebook within a python code.
您还可以使用该boar
包在 Python 代码中运行您的笔记本。
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.
如果您更新笔记本,则不必再次将其转换为 python 文件。
More information at:
更多信息请访问:
https://github.com/alexandreCameron/boar/blob/master/USAGE.md
https://github.com/alexandreCameron/boar/blob/master/USAGE.md