[根据@mobius 饺子的评论更新]
找到你的配置文件:
Jupyter / ipython >= 4.0
jupyter --config-dir
ipython <4.0
ipython locate profile default
如果你需要一个新的配置:
Jupyter / ipython >= 4.0
jupyter notebook --generate-config
ipython <4.0
ipython profile create
在此目录中,将有一个名为 的文件,将ipython 的 GitHub 问题页面[jupyter | ipython]_notebook_config.py
中的以下代码放入该文件中:
import os
from subprocess import check_call
c = get_config()
def post_save(model, os_path, contents_manager):
"""post-save hook for converting notebooks to .py scripts"""
if model['type'] != 'notebook':
return # only do this for notebooks
d, fname = os.path.split(os_path)
check_call(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
对于 Jupyter,在 check_call 中替换ipython
为jupyter
。
请注意,有一个相应的“预保存”钩子,并且您可以调用任何子进程或在那里运行任何任意代码……如果您想做任何花哨的事情,例如首先检查某些条件、通知 API 使用者或添加git commit 保存的脚本。
干杯,
-t。