以编程方式将ipython笔记本另存为脚本 [英] Save ipython notebook as script programmatically

查看:56
本文介绍了以编程方式将ipython笔记本另存为脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出色的ipython笔记本具有方便的--script命令行标志,该标志自动将笔记本的副本保存为.py脚本文件(删除任何标题和降价单元格).打开笔记本后,是否可以从笔记本本身内部打开此功能?显然,%config魔术无法使用此选项.

The excellent ipython notebook has a handy --script command line flag that automatically saves a copy of the notebook as a .py script file (removing any header and markdown cells). Is there a way to switch this feature on from inside the notebook itself after the notebook is opened? Apparently, this option is not accessible to the %config magic.

有没有办法进行转换的单元格?我可以使用任何命令行工具进行转换,而仅在笔记本计算机上运行的shell命令中使用该命令行工具吗? (看来nbconvert不会输出到.py.)

Is there a way to have a cell that does this conversion? Is there any command-line tool I could use to do the conversion, and just have that in a shell command run from the notebook? (It seems that nbconvert does not output to .py.)

我问的原因是我有一个笔记本的git存储库,当用户自己更改笔记本时,我需要确保.py文件保持最新,因为.py文件用于创建c ++笔记本内容中的代码.但是我不能依靠用户设置--script标志,因为他们将永远忘记. (而且我也属于该组用户.)

The reason I ask is that I have a git repository of notebooks, and I need to make sure the .py files are kept up to date when users change the notebooks themselves because the .py files are used to create c++ code from the contents of the notebooks. But I can't rely on users to set the --script flag because they'll always forget. (And I include myself in that group of users.)

推荐答案

更好(至少出于我的目的):ipython尊重ipython_notebook_config.py文件的本地副本.所以我可以添加

Better yet (at least for my purposes): ipython respects local copies of the ipython_notebook_config.py file. So I can just add

c = get_config()
c.NotebookManager.save_script = True

到我的笔记本目录中的此类文件.显然,ipython首先读取~/.ipython/profile_default/ipython_notebook_config.py,然后读取该文件的本地副本.因此使用起来很安全,无需担心会破坏用户设置.

to such a file in my notebook directory. Apparently, ipython first reads ~/.ipython/profile_default/ipython_notebook_config.py, and then reads the local copy of that file. So it's safe to use without worrying about demolishing the user settings.

我从文档中完全不清楚这点,但是我只是尝试了一下,就可以了.

This was not at all clear to me from the documentation, but I just tried it and it worked.

这篇关于以编程方式将ipython笔记本另存为脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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