简体   繁体   English

有没有办法从 .py 文件命令行创建 .ipynb ?

[英]Is there a way to create a .ipynb from a .py file command line?

Is there a way to create a .ipynb from a .py file command line?有没有办法从 .py 文件命令行创建 .ipynb ?

Why I would want to do this: I want to refactore some code using a text editor.我为什么要这样做:我想使用文本编辑器重构一些代码。 My ipynb file is on a server, and it is way easier to transfer a .py file (generated by running ipython notebook with --script) that is tracked on a version control system (git) than ipynb.我的 ipynb 文件在服务器上,传输在版本控制系统 (git) 上跟踪的 .py 文件(通过使用 --script 运行 ipython notebook 生成)比 ipynb 更容易。

Ideally I want to do the following:理想情况下,我想做以下事情:

  • the Notebook is running with --script , so I have a .py file笔记本正在运行--script ,所以我有一个 .py 文件
  • commit the new version of the file, push from the server and pull on my machine提交文件的新版本,从服务器推送并拉到我的机器上
  • pull from my machine从我的机器上拉
  • edit on my machine在我的机器上编辑
  • commit, push from my machine and pull from the server提交,从我的机器推送并从服务器拉取
  • create the .ipynb file from the new .py file and replace the old one从新的 .py 文件创建 .ipynb 文件并替换旧文件

And I can't find any way to start on the server an iPython Notebook from a py file that is on the server.而且我找不到任何方法从服务器上的 py 文件在服务器上启动 iPython Notebook。

Sure, I can add the .ipynb to git, but there are good reasons not to do that.当然,我可以将 .ipynb 添加到 git,但有充分的理由不这样做。 I could also send files using ftps, but since it is something I do rather often I'd like to find a better solution.我也可以使用 ftps 发送文件,但由于这是我经常做的事情,我想找到更好的解决方案。

I believe it is covered here: https://stackoverflow.com/a/23292713/3025981 .我相信这里有介绍: https : //stackoverflow.com/a/23292713/3025981 I reproduce the snipped here:我在这里复制了剪辑:

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

From https://pypi.org/project/ipynb-py-convert/来自https://pypi.org/project/ipynb-py-convert/

pip install ipynb-py-convert

Example例子

ipynb-py-convert examples/plot.py examples/plot.ipynb

or或者

ipynb-py-convert examples/plot.ipynb examples/plot.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM