ipython notebook --script 已弃用.如何用后保存钩子替换? [英] ipython notebook --script deprecated. How to replace with post save hook?

查看:12
本文介绍了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 dumpling 的评论更新]

找到您的配置文件:

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.

请注意,有一个相应的预保存"钩子,并且您可以在那里调用任何子进程或运行任何任意代码...或者为保存的脚本添加一个 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天全站免登陆