【问题标题】:Export a notebook (.ipynb) as a .py file upon saving保存时将笔记本 (.ipynb) 导出为 .py 文件
【发布时间】:2017-07-11 21:16:15
【问题描述】:

我目前正在使用 Jupyter IPython Notebook。我想将我的笔记本置于版本控制之下。

这就是为什么当我保存和检查笔记本(.ipynb 文件)时,我希望更改也可以保存并同步到同一文件夹中相应的 python 脚本(.py 文件)中。 (见下图)

my_files

这与我使用的 Jupyter 版本有关吗?还是我必须编辑一个 config_file?

谢谢

【问题讨论】:

    标签: ipython-notebook jupyter


    【解决方案1】:

    您需要在您的配置目录中创建 jupyter_notebook_config.py。

    如果不存在,从主目录执行以下命令: ./jupyter notebook --generate-config

    在此文件中添加粘贴以下代码:

    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 and .html files."""
        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)
        check_call(['ipython', 'nbconvert', '--to', 'html', fname], cwd=d)
    
    c.FileContentsManager.post_save_hook = post_save
    

    然后您需要重新启动您的 jupyter 服务器,然后就可以了!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2014-06-11
    • 2015-05-12
    • 1970-01-01
    • 2022-06-26
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多