不推荐使用ipython notebook --script.如何用邮政储蓄钩代替? [英] ipython notebook --script deprecated. How to replace with post save hook?

查看:114
本文介绍了不推荐使用ipython notebook --script.如何用邮政储蓄钩代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用"ipython --script"为每个ipython笔记本自动保存.py文件,因此我可以使用它将类导入其他笔记本.但这最近停止工作,并且我收到以下错误消息:

I have been using "ipython --script" to automatically save a .py file for each ipython notebook so I can use it to import classes into other notebooks. But this recenty stopped working, and I get the following error message:

`--script` is deprecated. You can trigger nbconvert via pre- or post-save hooks:
ContentsManager.pre_save_hook
FileContentsManager.post_save_hook
A post-save hook has been registered that calls:
ipython nbconvert --to script [notebook]
which behaves similarly to `--script`.

据我了解,我需要设置一个保存后的钩子,但是我不知道该怎么做.有人可以解释吗?

As I understand this I need to set up a post-save hook, but I do not understand how to do this. Can someone explain?

推荐答案

[每个评论由@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

在此目录中,将有一个名为[jupyter | ipython]_notebook_config.py的文件,将来自 ipython的GitHub问题页面中:

Within this directory, there will be a file called [jupyter | ipython]_notebook_config.py, put the following code from ipython's GitHub issues page in that file:

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.

For Jupyter, replace ipython with jupyter in check_call.

请注意,有一个相应的预保存"钩子,并且您可以在其中调用任何子进程或运行任何代码...如果您想做任何想做的事情,例如先检查某些条件,通知API使用者,或为保存的脚本添加git commit.

Note that there's a corresponding 'pre-save' hook, and also that you can call any subprocess or run any arbitrary code there...if you want to do any thing fancy like checking some condition first, notifying API consumers, or adding a git commit for the saved script.

干杯

-t.

这篇关于不推荐使用ipython notebook --script.如何用邮政储蓄钩代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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