[英]How to run an .ipynb Jupyter Notebook from terminal?
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.我在 a.ipynb 文件中有一些代码,并且已经到了我真的不需要 IPython Notebook 的“交互”功能的地步。 I would like to just run it straight from a Mac Terminal Command Line.我想直接从 Mac 终端命令行运行它。
Basically, if this were just a.py file, I believe I could just do python filename.py from the command line.基本上,如果这只是一个 .py 文件,我相信我可以从命令行执行 python filename.py。 Is there something similar for a.ipynb file? a.ipynb 文件有类似的东西吗?
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 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>
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 mistune package:您可能需要安装 python mistune包:
sudo pip install -U mistune
In your Terminal run ipython:在您的终端中运行 ipython:
ipython
then locate your script and put there:然后找到您的脚本并将其放在那里:
%run your_script.ipynb
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
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 清除当前文件的输出并保存到位,覆盖现有笔记本。
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."作者的注释“这个项目在 Jupyter 的执行 API 之前开始,现在是从命令行运行笔记本的推荐方式。考虑 runipy 已弃用和未维护。” – Sebastian Palma – 塞巴斯蒂安·帕尔马
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.您也可以尝试 cronjob。 All information is here所有信息都在这里
Using ipython :使用ipython :
ipython --TerminalIPythonApp.file_to_run=<notebook>.ipynb
Normally, I would prefer this option as it is really self-describing.通常,我更喜欢这个选项,因为它真的是自我描述的。 If you prefer to use less characters, use:如果您喜欢使用较少的字符,请使用:
ipython -c "%run <notebook>.ipynb"
which is basically what Keto already suggested (unfortunately a little bit hidden) as a comment .这基本上是Keto已经建议的(不幸的是有点隐藏)作为评论。
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
From the terminal run从终端运行
jupyter nbconvert --execute --to notebook --inplace --allow-errors --ExecutePreprocessor.timeout=-1 my_nb.ipynb
The default timeout is 30 seconds.默认超时为 30 秒。 -1 removes the restriction. -1 取消限制。
If you wish to save the output notebook to a new notebook you can use the flag --output my_new_nb.ipynb
如果您希望将输出笔记本保存到新笔记本,您可以使用标志--output my_new_nb.ipynb
I had the same problem and I found papermill .我遇到了同样的问题,我找到了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.它还具有其他方便的选项,例如将输出文件保存到 Amazon S3、Google Cloud 等。有关更多信息,请参阅页面。
You can also use jupytext
https://jupytext.readthedocs.io/en/latest/index.html .您还可以使用jupytext
https://jupytext.readthedocs.io/en/latest/index.html 。
This allows you to pair your notebook.这允许您配对您的笔记本电脑。 So for each ipynb
file you have a .py
file as well with some comments.因此,对于每个ipynb
文件,您都有一个.py
文件以及一些注释。 The .py
file can be executed as usual. .py
文件可以照常执行。 You can enjoy benefits of two worlds with the cost of one extra file though.不过,您可以通过一个额外文件的成本享受两个世界的好处。
Oh, and by the way if you are using version control you can only commit .py
files with a nice diff instead of the ugly .ipynb
diffs.哦,顺便说一句,如果您使用版本控制,您只能提交具有良好差异的.py
文件,而不是丑陋的.ipynb
差异。
(The syntax in the .py
files is similar to Databricks notebooks iy you are familiar with them...) ( .py
文件中的语法类似于 Databricks 笔记本,因为您熟悉它们......)
In a batch file paste the below.在批处理文件中粘贴以下内容。 Use ipython to run.ipynb files.使用 ipython 运行.ipynb 文件。
@echo on
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
ipython "path\test.ipynb"
pause
I've made some research on this topic and wrote an article on 4 ways to run Jupyter Notebook in command line below is summary of my findings.我对这个主题做了一些研究,并写了一篇关于在命令行中运行 Jupyter Notebook 的 4 种方法的文章,下面是我的发现摘要。
nbconvert
1.使用nbconvert
The nbconvert package is already installed in Jupyter Notebook. nbconvert包已安装在 Jupyter Notebook 中。 It can be used to execute notebook.它可以用来执行notebook。 Additionally it has many features:此外它还有很多特点:
Example notebook execution:笔记本执行示例:
jupyter nbconvert --execute --to notebook --allow-errors your-notebook.ipynb
The above command will output your-notebook.nbconvert.ipynb
file and will execute all cells even with errors.上面的命令将输出your-notebook.nbconvert.ipynb
文件,即使有错误也会执行所有单元格。
papermill
2.使用papermill
The papermill allows you to parametrize notebook. papermill允许您参数化笔记本。 You can define variables as parameters (with cell tags).您可以将变量定义为参数(使用单元格标签)。
Example command:示例命令:
papermill -p name Piotrek your-notebook.ipynb output-notebook.ipynb
Example notebook with injected parameters:带有注入参数的示例笔记本:
.py
script 3. 手动下载 notebook 为.py
脚本There is an option to manually download notebook as .py
script:有一个选项可以手动将笔记本下载为.py
脚本:
After download you can add execution rights to the file and run it as a command line script.下载后,您可以为该文件添加执行权限并将其作为命令行脚本运行。
jupytext
4.使用jupytext
The jupytext package allows you to synchronize .ipynb
file with .py
file. jupytext包允许您将.ipynb
文件与.py
文件同步。 You don't need to manually convert notebook to script.您无需手动将笔记本转换为脚本。
There is now a jupyter run
subcommand that will execute a notebook.现在有一个jupyter run
子命令可以执行笔记本。
jupyter run notebook.ipynb
More on this command can be found in the Jupyter documentation .有关此命令的更多信息,请参阅 Jupyter 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.