自动将Jupyter Notebook转换为.py [英] Automatically convert jupyter notebook to .py

查看:775
本文介绍了自动将Jupyter Notebook转换为.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对此有一些疑问,但我还没有发现足够强大的方法.

I know there have been a few questions about this but I have not found anything robust enough.

当前,我正在从终端使用创建.py的命令,然后将其移动到另一个文件夹:

Currently I am using, from terminal, a command that creates .py, then moves them to another folder:

jupyter nbconvert --to script '/folder/notebooks/notebook.ipynb' &&  \
mv ./folder/notebooks/*.py ./folder/python_scripts && \

然后,工作流程是在笔记本中编写代码,并使用git status检查自上次提交以来发生的更改,创建可能数量庞大的nbconvert命令,然后全部移动.

The workflow then is to code in a notebook, check with git status what changed since last commit, create a potentially huge number of nbconvert commands, then move them all.

我想在此答案中使用类似!jupyter nbconvert --to script found 的东西,但不使用可创建python的单元格文件显示在.py本身中.

I would like to use something like !jupyter nbconvert --to scriptfound in this answer, but without the cell that crates the python file appearing in the .py itself.

因为如果出现该行,我的代码将永远无法正常工作.

Because if that line appears, my code won't ever work right.

那么,有没有适当的方法来解决这个问题?可以自动执行,而无需手动复制文件名,创建命令,执行并重新启动的程序.

So, is there a proper way of dealing with this problem? One that can be automated, and not manually copying files names, creating the command, executing and then starting again.

推荐答案

另一种方法是使用 Jupytext 作为jupyter安装的扩展名(可以很容易地点安装).

Another way would be to use Jupytext as extension for your jupyter installation (can be easily pip installed).

Jupytext描述(请参见github页)

Jupytext Description (see github page)

您一直希望Jupyter笔记本是纯文本文档吗? 希望您可以在自己喜欢的IDE中编辑它们吗?并弄清楚 版本控制时有意义的差异?那... Jupytext可能很好 成为您要寻找的工具!

Have you always wished Jupyter notebooks were plain text documents? Wished you could edit them in your favorite IDE? And get clear and meaningful diffs when doing version control? Then... Jupytext may well be the tool you're looking for!

它将使配对的笔记本与.py文件保持同步.然后,您只需要移动您的.py文件或gitignore笔记本,例如作为可能的工作流程.

It will keep paired notebooks in sync with .py files. You then just need to move your .py files or gitignore the notebooks for example as possible workflows.

这篇关于自动将Jupyter Notebook转换为.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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