我正在考虑使用*.ipynb文件作为事实来源,并以编程方式将它们“编译”成 .py 文件以用于计划的作业/任务。
我理解的唯一方法是通过 GUI。有没有办法通过命令行来做到这一点?
我正在考虑使用*.ipynb文件作为事实来源,并以编程方式将它们“编译”成 .py 文件以用于计划的作业/任务。
我理解的唯一方法是通过 GUI。有没有办法通过命令行来做到这一点?
如果不想每次保存都输出 Python 脚本,或者不想重启 IPython 内核:
在命令行上,您可以使用nbconvert
:
$ jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
作为一个小技巧,您甚至可以通过预先挂起!
(用于任何命令行参数)在IPython 笔记本中调用上述命令。笔记本内部:
!jupyter nbconvert --to script config_template.ipynb
在添加--to script
之前,该选项是or ,但在朝着与语言无关的笔记本系统发展的过程中被重命名。--to python
--to=python
如果要将*.ipynb
当前目录中的所有文件转换为 python 脚本,可以运行如下命令:
jupyter nbconvert --to script *.ipynb
这是一种不使用 ipython 从 V3 或 V4 ipynb 提取代码的快速而肮脏的方法。它不检查细胞类型等。
import sys,json
f = open(sys.argv[1], 'r') #input.ipynb
j = json.load(f)
of = open(sys.argv[2], 'w') #output.py
if j["nbformat"] >=4:
for i,cell in enumerate(j["cells"]):
of.write("#cell "+str(i)+"\n")
for line in cell["source"]:
of.write(line)
of.write('\n\n')
else:
for i,cell in enumerate(j["worksheets"][0]["cells"]):
of.write("#cell "+str(i)+"\n")
for line in cell["input"]:
of.write(line)
of.write('\n\n')
of.close()
遵循前面的示例,但使用新的 nbformat lib 版本:
import nbformat
from nbconvert import PythonExporter
def convertNotebook(notebookPath, modulePath):
with open(notebookPath) as fh:
nb = nbformat.reads(fh.read(), nbformat.NO_CONVERT)
exporter = PythonExporter()
source, meta = exporter.from_notebook_node(nb)
with open(modulePath, 'w+') as fh:
fh.writelines(source.encode('utf-8'))
Jupytext很适合在您的工具链中进行此类转换。它不仅允许从笔记本转换为脚本,还可以从脚本再次返回到笔记本。甚至以执行形式制作该笔记本。
jupytext --to py notebook.ipynb # convert notebook.ipynb to a .py file
jupytext --to notebook notebook.py # convert notebook.py to an .ipynb file with no outputs
jupytext --to notebook --execute notebook.py # convert notebook.py to an .ipynb file and run it
您可以从 IPython API 执行此操作。
from IPython.nbformat import current as nbformat
from IPython.nbconvert import PythonExporter
filepath = 'path/to/my_notebook.ipynb'
export_path = 'path/to/my_notebook.py'
with open(filepath) as fh:
nb = nbformat.reads_json(fh.read())
exporter = PythonExporter()
# source is a tuple of python source code
# meta contains metadata
source, meta = exporter.from_notebook_node(nb)
with open(export_path, 'w+') as fh:
fh.writelines(source)
我知道这是一个旧线程。我遇到了同样的问题,想通过命令行将 .pynb 文件转换为 .py 文件。
我的搜索将我带到了ipynb-py-convert
通过以下步骤,我能够获得 .py 文件
> ipynb-py-convert YourFileName.ipynb YourFilename.py
例如:。ipynb-py-convert 开始使用-kaggle-titanic-problem.ipynb 开始使用-kaggle-titanic-problem.py
上面的命令将创建一个名为“YourFileName.py”的python脚本,根据我们的示例,它将创建getting-started-with-kaggle-titanic-problem.py
文件
用于递归地将当前目录中的所有 *.ipynb 格式文件转换为 python 脚本:
for i in *.ipynb **/*.ipynb; do
echo "$i"
jupyter nbconvert "$i" "$i"
done
下面的示例将调用 Iron Python Notebooka_notebook.ipynb
转换为 Python 脚本,称为a_python_script.py
省略带有关键字remove
的单元格,我手动将其添加到我不想在脚本中结束的单元格中,省略可视化和其他步骤一旦我完成了笔记本,我就不需要被脚本执行了。
import nbformat as nbf
from nbconvert.exporters import PythonExporter
from nbconvert.preprocessors import TagRemovePreprocessor
with open("a_notebook.ipynb", 'r', encoding='utf-8') as f:
the_notebook_nodes = nbf.read(f, as_version = 4)
trp = TagRemovePreprocessor()
trp.remove_cell_tags = ("remove",)
pexp = PythonExporter()
pexp.register_preprocessor(trp, enabled= True)
the_python_script, meta = pexp.from_notebook_node(the_notebook_nodes)
with open("a_python_script.py", 'w', encoding='utf-8') as f:
f.writelines(the_python_script)
使用 nbconvert 6.07 和 jupyter 客户端 6.1.12:
将 jupyter notebook 转换为 python 脚本
$ jupyter nbconvert mynotebook.ipynb --to python
将 jupyter notebook 转换为指定输出文件名的 python 脚本
$ jupyter nbconvert mynotebook.ipnb --to python --output myscript.py
有一个非常棒的包,叫做nb_dev,它是为在 Jupyter Notebooks 中编写 Python 包而设计的。就像nbconvert,
它可以将笔记本变成 .py 文件一样,但它更加灵活和强大,因为它具有许多不错的附加创作功能,可帮助您在 PyPI 上开发测试、文档和注册包。它是由 fast.ai 开发的。
它有一点学习曲线,但文档很好,总体上并不难。
我遇到了这个问题,并试图在网上找到解决方案。虽然我找到了一些解决方案,但它们仍然存在一些问题,例如,Untitled.txt
当您从仪表板启动新笔记本时,烦人的自动创建。
所以最终我写了自己的解决方案:
import io
import os
import re
from nbconvert.exporters.script import ScriptExporter
from notebook.utils import to_api_path
def script_post_save(model, os_path, contents_manager, **kwargs):
"""Save a copy of notebook to the corresponding language source script.
For example, when you save a `foo.ipynb` file, a corresponding `foo.py`
python script will also be saved in the same directory.
However, existing config files I found online (including the one written in
the official documentation), will also create an `Untitile.txt` file when
you create a new notebook, even if you have not pressed the "save" button.
This is annoying because we usually will rename the notebook with a more
meaningful name later, and now we have to rename the generated script file,
too!
Therefore we make a change here to filter out the newly created notebooks
by checking their names. For a notebook which has not been given a name,
i.e., its name is `Untitled.*`, the corresponding source script will not be
saved. Note that the behavior also applies even if you manually save an
"Untitled" notebook. The rationale is that we usually do not want to save
scripts with the useless "Untitled" names.
"""
# only process for notebooks
if model["type"] != "notebook":
return
script_exporter = ScriptExporter(parent=contents_manager)
base, __ = os.path.splitext(os_path)
# do nothing if the notebook name ends with `Untitled[0-9]*`
regex = re.compile(r"Untitled[0-9]*$")
if regex.search(base):
return
script, resources = script_exporter.from_filename(os_path)
script_fname = base + resources.get('output_extension', '.txt')
log = contents_manager.log
log.info("Saving script at /%s",
to_api_path(script_fname, contents_manager.root_dir))
with io.open(script_fname, "w", encoding="utf-8") as f:
f.write(script)
c.FileContentsManager.post_save_hook = script_post_save
要使用此脚本,您可以将其添加到~/.jupyter/jupyter_notebook_config.py
:)
请注意,您可能需要重新启动 jupyter notebook / lab 才能工作。
在我工作的薄荷 [ubuntu] 系统上,即使已经安装了 jupyter 并且笔记本也可以工作,但在我单独执行之前jupyter nbconvert --to script
给出了错误no file/directory
sudo apt-get install jupyter-nbconvert
然后一切都很好,转换。我只是想添加这个以防有人遇到相同的错误(对我来说这很令人困惑,因为我认为没有文件错误指的是笔记本,它肯定在本地目录中,我花了一段时间才意识到子命令不是安装)。
魔术%notebook foo.ipynb
命令会将当前的 IPython 导出到“foo.ipynb”。
输入更多信息%notebook?