[英]How to execute a * .PY file from a * .IPYNB file on the Jupyter notebook?
[英]Run Jupyter Notebook (.ipynb) from command line as if it were a .py file
我正在我的本地机器上编写一个 Jupyter notebook,它最终将在远程服务器(运行 Ubuntu)上运行。 每次我需要进行更改时,我都必须将笔记本导出为.py
文件,然后从服务器的命令行调用它。
我希望能够即时运行它,调用一个命令,该命令获取当前.ipynb
文件并在命令行上执行它,就好像它是一个.py
一样,显示您期望的所有打印语句和输出如果.py
运行了。 我认为nbconverter
可能会使用类似以下命令的方法来解决问题:
jupyter nbconvert --to script --execute nbconvert_test.ipynb
事实证明,这并没有像我希望的那样将.ipynb
转换为要在命令行上执行的.py
文件,而是在我必须运行的同一目录中创建一个名为nbconvert_test.py
的新文件在一个单独的命令中。 我真的很想在每次进行微小更改时都阻止创建该文件,并跳过命令行上的额外步骤。
任何帮助表示赞赏!
您可以将 jupyter nbconvert 发送到搁浅输出并将其通过管道传输到 python。
jupyter nbconvert --to script --execute --stdout test_nbconvert.ipynb | python
解决方法是一个包含三个部分的小 shell 脚本
创建文件runnb.sh
#!/bin/sh
# First argument is the notebook you would like to run
notebook=$1
scriptname="$(basename $notebook .ipynb)".py
jupyter nbconvert --to script --execute ${notebook} && python ${scriptname}
rm ${scriptname}
像这样使用:
$ ./runnb.sh nbconvert_test.ipynb
编辑: 根据这个答案,这个命令应该做的很好jupyter nbconvert --execute test_nbconvert.ipynb
(只是离开了--to
标志
使用boar
包,您可以在 python 代码中运行您的笔记本,使用:
from boar.running import run_notebook
outputs = run_notebook("nbconvert_test.ipynb")
有关详细信息,请参阅:
https://github.com/alexandreCameron/boar/blob/master/USAGE.md
我有一个类似的错误 xyz.py not found for rm command 然后我在 conda 虚拟环境中运行脚本并且它有效。
conda activate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.