从命令行运行Jupyter Notebook(.ipynb),就好像它是一个.py文件一样 [英] Run Jupyter Notebook (.ipynb) from command line as if it were a .py file

查看:918
本文介绍了从命令行运行Jupyter Notebook(.ipynb),就好像它是一个.py文件一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在本地计算机上编写Jupyter笔记本,该笔记本最终将在远程服务器(运行Ubuntu)上运行.每次需要进行更改时,都必须将笔记本导出为.py文件,然后从服务器的命令行对其进行调用.

I am authoring a Jupyter notebook on my local machine that will eventually be run on a remote server (which is running 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.

我希望能够即时运行此命令,调用一个命令以获取当前.ipynb文件,并在命令行上将其执行为好像是.py一样,并显示所有打印语句和如果您希望运行.py,则输出.我以为nbconverter可以使用以下命令来达到目的:

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. I thought nbconverter might do the trick using something like the following command:

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

结果是,这不会将.ipynb转换为要在命令行上执行的.py文件,而是在与我相同的目录中创建了一个名为nbconvert_test.py的新文件.然后必须在单独的命令中运行.我真的很想防止每次更改时都创建该文件,并跳过命令行上的额外步骤.

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. 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.

感谢您的帮助!

推荐答案

您可以将jupyter nbconvert发送到绞合输出,然后将其通过管道传输到python.

You can send the jupyter nbconvert to stranded output and pipe that to python.

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

这篇关于从命令行运行Jupyter Notebook(.ipynb),就好像它是一个.py文件一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆