简体   繁体   English

如何从 Jupyter notebook 获取原始代码?

[英]How to get raw code from a Jupyter notebook?

It would be really handy to have a button to copy raw code from a Jupyter notebook to the clipboard so it can be dumped into a text file, .py/.R script, or straight into the terminal (a button like github has to copy to clipboard)如果有一个按钮可以将原始代码从 Jupyter notebook 复制到剪贴板,这样它就可以转储到文本文件、.py/.R 脚本或直接进入终端(像 github 这样的按钮必须复制到剪贴板)

Does a button (or even a chrome add on) exist?是否存在按钮(甚至是 chrome 插件)?

Obviously you could copy/paste sections manually into a txt file, but that's laborious for long notebooks/markdowns显然,您可以手动将部分复制/粘贴到 txt 文件中,但这对于长笔记本/降价文件来说很费力

Yes,there is a button:是的,有一个按钮:

File > Download as > Python (.py)文件 > 下载为 > Python (.py)

There is a more handy way for Mac OS users.对于 Mac OS 用户,有一种更方便的方法。

By installing an Automator script converting the ipynb file to py first, then automatically copy the content to the clipboard with one right-click and select the script in the Quick Action part.通过安装一个Automator脚本, ipynb文件转换为py ,然后通过单击鼠标右键自动将内容复制到剪贴板,并在Quick Action部分选择脚本。

Github link Github链接

在此处输入图像描述

You can use the magic command %%writefile to save as py file您可以使用魔术命令 %%writefile 保存为 py 文件

%%writefile "directory/to/file/filename.py"

However, you should place this command at the top of the cell, otherwise it will throw an error.但是,您应该将此命令放在单元格的顶部,否则会引发错误。

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

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