Python 我可以在命令行中运行 Jupyter 笔记本单元吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35471894/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:29:36  来源:igfitidea点击:

Can I run Jupyter notebook cells in commandline?

pythoncommand-lineipythonjupyter

提问by pylang

I am deploying a Python package, and I would like to run a simple test to see if all cells in my notebook will run without errors. I would like to test this via commandline as I have issues running a notebook in virtualenv. Is there are simple command-line way to test this?

我正在部署一个 Python 包,我想运行一个简单的测试,看看我的笔记本中的所有单元格是否都能正常运行。我想通过命令行测试这个,因为我在virtualenv. 有没有简单的命令行方法来测试这个?



Note to the moderator: this question has been marked as a duplicate of How to run an .ipynb Jupyter Notebook from terminal? . However, this question was posted (asked Feb 18 '16 at 2:49) several days before that one (asked Feb 22 '16 at 3:35). At most, that post might be marked as a duplicate, and if deemed so, an appropriate action would be to merge the two questions, maintaining this, the original, as the master.

版主注意:此问题已标记为如何从终端运行 .ipynb Jupyter Notebook? . 但是,这个问题是在该问题前几天发布的(2016 年 2 月 18 日 2:49 提问)(2016 年 2 月 22 日 3:35 提问)。最多,该帖子可能会被标记为重复,如果被认为是这样,则适当的操作是合并两个问题,将这个原始问题保持为 master

However, these questions may notbe duplicates (the intent of the other author is unclear). Regardless, this question and it's answers specifically address executing cells within a jupyter notebook from the terminal, not simply converting notebooks to python files.

但是,这些问题可能不会重复(其他作者的意图不清楚)。无论如何,这个问题及其答案专门针对从终端执行 jupyter notebook 中的单元格,而不是简单地将 notebooks 转换为 python 文件。

采纳答案by RajeshM

You can use runipyto do this.

您可以使用runipy来执行此操作。

runipywill run all cells in a notebook. If an error occurs, the process will stop.

runipy将运行笔记本中的所有单元格。如果发生错误,该过程将停止。

$ pip install runipy

$ runipy MyNotebook.ipynb

$ pip install runipy

$ runipy MyNotebook.ipynb

There are also commands for saving the output file as a notebook or an html report:

还有一些命令可以将输出文件保存为笔记本或 html 报告:

$ runipy MyNotebook.ipynb OutputNotebook.ipynb

$ runipy MyNotebook.ipynb --html report.html

$ runipy MyNotebook.ipynb OutputNotebook.ipynb

$ runipy MyNotebook.ipynb --html report.html

回答by geekazoid

nbconvert(a jupyter tool for notebook conversion) allows you to do this without any extra packages:

nbconvert(用于笔记本转换的 jupyter 工具)允许您在没有任何额外包的情况下执行此操作:

Just go to your terminal and type

只需转到您的终端并输入

$ jupyter nbconvert --to notebook --inplace --execute mynotebook.ipynb

$ jupyter nbconvert --to notebook --inplace --execute mynotebook.ipynb

Source

来源

(Thanks Stephan for suggesting the --inplaceflag)

(感谢 Stephan 建议--inplace国旗)

回答by Giacomo

You can also try papermillwhich allows you to execute notebooks from command line, and also pass parameters:

您还可以尝试纸厂,它允许您从命令行执行笔记本,还可以传递参数:

For example:

例如:

$ papermill mynotebook.ipynb mynotebook_output.ipynb -p start "2017-11-01" -p end "2017-11-30"

You can also run it without passing any parameter.

您也可以在不传递任何参数的情况下运行它。