将 json ipython notebook(.ipynb) 转换为 .py 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37797709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert json ipython notebook(.ipynb) to .py file
提问by Nitesh Selkari
How do you convert an IPython notebook file (json with .ipynb
extension) into a regular .py
module?
如何将 IPython 笔记本文件(带有.ipynb
扩展名的json )转换为常规.py
模块?
回答by kikocorreoso
From the notebook menu you can save the file directly as a python script. Go to the 'File' option of the menu, then select 'Download as' and there you would see a 'Python (.py)' option.
从 notebook 菜单中,您可以将文件直接保存为 python 脚本。转到菜单的“文件”选项,然后选择“下载为”,然后您会看到“Python (.py)”选项。
Another option would be to use nbconvert from the command line:
另一种选择是从命令行使用 nbconvert:
jupyter nbconvert --to script 'my-notebook.ipynb'
Have a look here.
看看这里。
回答by Kush
According to https://ipython.org/ipython-doc/3/notebook/nbconvert.htmlyou are looking for the nbconvert command with the --to script option.
根据https://ipython.org/ipython-doc/3/notebook/nbconvert.html,您正在寻找带有 --to 脚本选项的 nbconvert 命令。
ipython nbconvert notebook.ipynb --to script
回答by Srikar Appalaraju
In short:This command-line option converts mynotebook.ipynb
to python
code:
简而言之:此命令行选项转换mynotebook.ipynb
为python
代码:
jupyter nbconvert mynotebook.ipynb --to python
jupyter nbconvert mynotebook.ipynb --to python
note:this is different from aboveanswer. ipython
has been renamed to jupyter
. the old executable name (ipython) is deprecated.
注意:这与上面的答案不同。ipython
已更名为jupyter
. 旧的可执行文件名称 (ipython) 已弃用。
More details:jupyter
command-line has an nbconvert
argument which helps convert notebook files (*.ipynb)
to various other formats.
更多细节:jupyter
命令行有一个nbconvert
参数可以帮助将笔记本文件转换(*.ipynb)
为各种其他格式。
You could even convert it to any one of these formats using the same command but different --to
option:
您甚至可以使用相同的命令但不同的--to
选项将其转换为这些格式中的任何一种:
- asciidoc
- custom
- html
- latex. (Awesome if you want to paste code in conference/journal papers).
- markdown
- notebook
- python
- rst
- script
- slides. (Whooh! Convert to slides for easy presentation )
- asciidoc
- 风俗
- html
- 乳胶。(如果您想在会议/期刊论文中粘贴代码,那就太棒了)。
- 降价
- 笔记本
- Python
- 第一个
- 脚本
- 幻灯片。(哇!转换为幻灯片以便于演示)
the same command jupyter nbconvert --to latex mynotebook.ipynb
同样的命令 jupyter nbconvert --to latex mynotebook.ipynb
For more see jupyter nbconvert --help
. There are extensive options to this. You could even to execute the code first before converting, different log-level options etc.
有关更多信息,请参阅jupyter nbconvert --help
。对此有广泛的选择。您甚至可以在转换之前先执行代码、不同的日志级别选项等。
回答by sebuhi
well first of all you need to install this packege below:
首先你需要在下面安装这个包:
sudo apt install ipython
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
two option is avaliable either --to python or --to=python mine was like this works fine: jupyter nbconvert --to python while.ipynb
两个选项可用 --to python 或 --to=python 我的就像这样工作正常:jupyter nbconvert --to python while.ipynb
jupyter nbconvert --to python while.ipynb
[NbConvertApp] Converting notebook while.ipynb to python [NbConvertApp] Writing 758 bytes to while.py
[NbConvertApp] 将 notebook while.ipynb 转换为 python [NbConvertApp] 将 758 字节写入 while.py
pip3 install ipython
if it does not work for you try, by pip3.
如果它对您不起作用,请通过 pip3 尝试。
pip3 install ipython
回答by Blade
- Go to https://jupyter.org/
- click on nbviewer
- Enter the location of your file and render it.
- Click on view as code (shown as < />)
- 去https://jupyter.org/
- 单击 nbviewer
- 输入文件的位置并渲染它。
- 点击查看为代码(显示为< />)
回答by Syrtis Major
You can use the following script to convert jupyter notebook to Python script, or view the code directly.
可以使用以下脚本将jupyter notebook转为Python脚本,或者直接查看代码。
To do this, write the following contents into a file cat_ipynb
, then chmod +x cat_ipynb
.
为此,请将以下内容写入文件cat_ipynb
,然后将chmod +x cat_ipynb
.
#!/usr/bin/env python
import sys
import json
for file in sys.argv[1:]:
print('# file: %s' % file)
print('# vi: filetype=python')
print('')
code = json.load(open(file))
for cell in code['cells']:
if cell['cell_type'] == 'code':
print('# -------- code --------')
for line in cell['source']:
print(line, end='')
print('\n')
elif cell['cell_type'] == 'markdown':
print('# -------- markdown --------')
for line in cell['source']:
print("#", line, end='')
print('\n')
Then you can use
然后你可以使用
cat_ipynb your_notebook.ipynb > output.py
cat_ipynb your_notebook.ipynb > output.py
Or show it with vi
directly
或者vi
直接用
cat_ipynb your_notebook.ipynb | view -
cat_ipynb your_notebook.ipynb | view -
回答by user1460675
You definitely can achieve that with nbconvert using the following command:
您绝对可以使用以下命令通过 nbconvert 实现这一点:
jupyter nbconvert --to python while.ipynb
However, having used it personally I would advise against it for several reasons:
但是,个人使用它后,我建议不要这样做,原因有几个:
- It's one thing to be able to convert to simple Python code and another to have all the right abstractions, classes access and methods set up. If the whole point of you converting your notebook code to Python is getting to a state where your code and notebooks are maintainable for the long run, then nbconvert alone will not suffice. The only way to do that is by manually going through the codebase.
- Notebooks inherently promote writing code which is not maintainable (https://docs.google.com/presentation/d/1n2RlMdmv1p25Xy5thJUhkKGvjtV-dkAIsUXP-AL4ffI/edit#slide=id.g3d7fe085e7_0_21). Using nbconvert on top might just prove to be a bandaid. Specific examples of where it promotes not-so-maintainable code are imports might be sprayed throughout, hard coded paths are not in one simple place to view, class abstractions might not be present, etc.
- nbconvert still mixes execution code and library code.
- Comments are still not present (probably were not in the notebook).
- There is still a lack of unit tests etc.
- 能够转换为简单的 Python 代码是一回事,而拥有所有正确的抽象、类访问和方法设置又是另一回事。如果您将笔记本代码转换为 Python 的全部目的是使您的代码和笔记本可以长期维护,那么仅使用 nbconvert 是不够的。做到这一点的唯一方法是手动浏览代码库。
- Notebooks 本质上促进编写不可维护的代码(https://docs.google.com/presentation/d/1n2RlMdmv1p25Xy5thJUhkKGvjtV-dkAIsUXP-AL4ffI/edit#slide=id.g3d7fe085e7_0_21)。在顶部使用 nbconvert 可能只是一个创可贴。它促进不那么可维护的代码的具体例子是导入可能会被喷洒,硬编码路径不在一个简单的地方可以查看,类抽象可能不存在等。
- nbconvert 仍然混合了执行代码和库代码。
- 评论仍然不存在(可能不在笔记本中)。
- 仍然缺乏单元测试等。
So to summarize, there is not good way to out of the box convert python notebooks to maintainable, robust python modularized code, the only way is to manually do surgery.
所以总结一下,开箱即用的将python笔记本转换为可维护的、健壮的python模块化代码的方法并不好,唯一的方法是手动做手术。
回答by Wayne
回答by Vahe Grickory
Copy all the (''.ipynb) files in the Desired folder then execute:
复制 Desired 文件夹中的所有 (''.ipynb) 文件,然后执行:
import os
desired_path = 'C:\Users\Docs\Ipynb Covertor'
os.chdir(desired_path)
list_of_directory = os.listdir(desired_path)
for file in list_of_directory:
os.system('ipython nbconvert --to script ' + str(file))