简体   繁体   English

从命令行运行 Jupyter Notebook (.ipynb),就好像它是一个 .py 文件一样

[英]Run Jupyter Notebook (.ipynb) from command line as if it were a .py file

I am authoring a Jupyter notebook on my local machine that will eventually be run on a remote server (which is running Ubuntu).我正在我的本地机器上编写一个 Jupyter notebook,它最终将在远程服务器(运行 Ubuntu)上运行。 Every time I need to make a change I must export the notebook as a .py file and then call it from the command line of the server.每次我需要进行更改时,我都必须将笔记本导出为.py文件,然后从服务器的命令行调用它。

I'd like to be able to run this on the fly, calling one command that takes the current .ipynb file and executes it on the command line as if it were a .py , showing all the print statements and output you'd expect if the .py were run.我希望能够即时运行它,调用一个命令,该命令获取当前.ipynb文件并在命令行上执行它,就好像它是一个.py一样,显示您期望的所有打印语句和输出如果.py运行了。 I thought nbconverter might do the trick using something like the following command:我认为nbconverter可能会使用类似以下命令的方法来解决问题:

jupyter nbconvert --to script --execute nbconvert_test.ipynb

As it turnout, this does not convert the .ipynb to a .py file to be executed on the command line as I would like, but rather it creates a new file called nbconvert_test.py in the same directory which I would then have to run in a separate command.事实证明,这并没有像我希望的那样将.ipynb转换为要在命令行上执行的.py文件,而是在我必须运行的同一目录中创建一个名为nbconvert_test.py的新文件在一个单独的命令中。 I'd really like to prevent the creation of that file every time I make even a small change, and to skip the extra step on the command line.我真的很想在每次进行微小更改时都阻止创建该文件,并跳过命令行上的额外步骤。

Any help is appreciated!任何帮助表示赞赏!

You can send the jupyter nbconvert to stranded output and pipe that to python.您可以将 jupyter nbconvert 发送到搁浅输出并将其通过管道传输到 python。

jupyter nbconvert --to script --execute --stdout test_nbconvert.ipynb | python

A workaround is a small shell script that has three parts解决方法是一个包含三个部分的小 shell 脚本

  1. converting the notebook转换笔记本
  2. executing created script执行创建的脚本
  3. removing the script删除脚本

create a file runnb.sh创建文件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}

use as such:像这样使用:

$ ./runnb.sh nbconvert_test.ipynb

EDIT: According to this answer , this command should do just fine jupyter nbconvert --execute test_nbconvert.ipynb (just leav out the --to flag编辑: 根据这个答案,这个命令应该做的很好jupyter nbconvert --execute test_nbconvert.ipynb (只是离开了--to标志

With the boar package, you can run your notebook within a python code, using:使用boar包,您可以在 python 代码中运行您的笔记本,使用:

from boar.running import run_notebook

outputs = run_notebook("nbconvert_test.ipynb")

For more information, see:有关详细信息,请参阅:

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

I had a similar error xyz.py not found for rm command Then I ran the script inside the conda virtual environment and it worked.我有一个类似的错误 xyz.py not found for rm command 然后我在 conda 虚拟环境中运行脚本并且它有效。

conda activate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM