问题描述
我正在考虑使用 *.ipynb 文件作为事实来源,并以编程方式将它们编译"为 .py 文件以用于计划的作业/任务.
I'm looking at using the *.ipynb files as the source of truth and programmatically 'compiling' them into .py files for scheduled jobs/tasks.
我理解的唯一方法是通过 GUI.有没有办法通过命令行来做到这一点?
The only way I understand to do this is via the GUI. Is there a way to do it via command line?
推荐答案
如果不想每次保存都输出一个Python脚本,或者不想重启IPython内核:
If you don't want to output a Python script every time you save, or you don't want to restart the IPython kernel:
在命令行上,您可以使用nbconvert
:
On the command line, you can use nbconvert
:
$ jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
作为一个小技巧,您甚至可以在一个 IPython notebook 中调用上述命令,方法是在前面加上 !
(用于任何命令行参数).在笔记本内:
As a bit of a hack, you can even call the above command in an IPython notebook by pre-pending !
(used for any command line argument). Inside a notebook:
!jupyter nbconvert --to script config_template.ipynb
在 --to script
被添加之前,选项是 --to python
或 --to=python
,但它是 重命名,以实现与语言无关的笔记本系统.
Before --to script
was added, the option was --to python
or --to=python
, but it was renamed in the move toward a language-agnostic notebook system.
这篇关于如何通过命令行将 IPython Notebook 转换为 Python 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!