我正在寻找使用* .ipynb文件作为事实来源,并以编程方式将其“编译”为计划作业/任务的.py文件。
我了解的唯一方法是通过GUI。有办法通过命令行吗?
我正在寻找使用* .ipynb文件作为事实来源,并以编程方式将其“编译”为计划作业/任务的.py文件。
我了解的唯一方法是通过GUI。有办法通过命令行吗?
Answers:
如果不想在每次保存时都输出Python脚本,或者不想重新启动IPython内核:
在命令行上,您可以使用nbconvert
:
$ jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
有点技巧,您甚至可以通过预先挂起!
(用于任何命令行参数)在 IPython笔记本中调用上述命令。在笔记本内:
!jupyter nbconvert --to script config_template.ipynb
之前--to script
被加入,可以选择是--to python
或--to=python
,但它改名朝一个语言无关的笔记本电脑系统的举动。
jupyter
可以nbconvert
通过保存前或保存后钩子ContentsManager.pre_save_hook
abd 触发FileContentsManager.post_save_hook
。您将添加一个保存后的钩子jupyter nbconvert --to script [notebook]
jupyter nbconvert --to script /path/to/notebooks/*.ipynb
# In[ ]:
在脚本中输入类型,我希望它是干净的。有什么办法吗?
如果要将所有*.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()
newlines='\n'
在打开的输出文件调用中将as作为第三个参数添加。(Python 3.x)
遵循前面的示例,但具有新的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'))
您可以从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)
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
我遇到了这个问题,并试图在线找到解决方案。尽管我找到了一些解决方案,但是它们仍然存在一些问题,例如,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笔记本/实验室才能正常工作。
input
在cell_type
等于“代码”的地方迭代键。看看这个方案