使用nbconvert作为库运行预处理器 [英] Run preprocessor using nbconvert as a library
问题描述
我想用预处理器运行nbconvert,该预处理器将删除标记为"skip"的单元格.我可以从命令行执行此操作,但是当我尝试在笔记本中使用nbconvert API时,就会遇到问题.
I would like to run nbconvert with a preprocessor that removes cells marked with the tag "skip". I am able to do this from the command line, but when I try to use the nbconvert API within a notebook I run into problems.
按照文档中的示例,我得到了一个可以使用的笔记本.
Following the example in the documentation, I get a notebook to work with.
from urllib.request import urlopen
url = 'http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb'
response = urlopen(url).read().decode()
import nbformat
nb = nbformat.reads(response, as_version=4)
我将修改一个单元格,使其在输出中被跳过.
I'll modify one cell so it gets skipped in the output.
nb.cells[1].metadata = {'tags': ['skip']}
命令行
保存文件,然后从命令行运行nbconvert:
Command line
Saving the file, and then running nbconvert from the command line:
nbformat.write(nb, 'nb.ipynb')
%%bash
jupyter nbconvert --to latex \
--TagRemovePreprocessor.remove_cell_tags='{"skip"}' \
--TagRemovePreprocessor.enabled=True \
'nb.ipynb'
这有效.输出的nb.tex
文件不包含标记为跳过"的单元格.
This works. The output nb.tex
file does not contain the cell tagged "skip".
现在让我们改用API尝试一下.首先,无需任何预处理:
Now let's try it using the API instead. First, without any preprocessing:
import nbconvert
latex, _ = LatexExporter().from_notebook_node(nb)
print(latex[:25])
\ documentclass [11pt] {arti
\documentclass[11pt]{arti
再次,没问题.转换正常.
Again, no problem. The conversion is working.
现在,尝试使用与从命令行使用的相同的预处理器:
Now, trying to use the same preprocessor I used from the command line:
from traitlets.config import Config
c = Config()
c.RemovePreprocessor.remove_cell_tags = ('skip',)
c.LatexExporter.preprocessors = ['TagRemovePreprocessor']
LatexExporter(config=c).from_notebook_node(nb)
这次,我得到了:
ModuleNotFoundError:没有名为"TagRemovePreprocessor"的模块
ModuleNotFoundError: No module named 'TagRemovePreprocessor'
据我所知,此代码与文档中的代码示例,除了我使用的是Latex导出器而不是HTML.那为什么不起作用?
As far as I can see, this code matches the code sample in the documentation, except that I'm using the Latex exporter instead of HTML. So why isn't it working?
推荐答案
对于您的特定情况,我相信您可以通过以下方法解决此问题:c.RemovePreprocessor.remove_cell_tags = ('skip',)
->
c.TagRemovePreprocessor.remove_cell_tags = ('skip',)
For your particular case, I believe you can resolve the issue by changing: c.RemovePreprocessor.remove_cell_tags = ('skip',)
->
c.TagRemovePreprocessor.remove_cell_tags = ('skip',)
为了像我通过搜索一样遇到此线程的其他人受益
For the benefit of others that come across this thread as I did by searching
ModuleNotFoundError: No module named 'TagRemovePreprocessor'
TagRemovePreprocessor
存在一个公开问题,该问题导致除HTMLExporter
(和LatexExporter
?)自动禁用此预处理器.
There is an open issue with TagRemovePreprocessor
that causes all exporters other than the HTMLExporter
(and LatexExporter
?) to automatically disable this preprocessor.
在我的情况下,我试图使用NotebookExporter
,需要显式启用预处理器并更改预处理级别,如下所示:
In my case, I was attempting to use the NotebookExporter
and needed to explicitly enable the preprocessor and change the preprocessing level like so:
import json
from traitlets.config import Config
from nbconvert import NotebookExporter
import nbformat
c = Config()
c.TagRemovePreprocessor.enabled=True # Add line to enable the preprocessor
c.TagRemovePreprocessor.remove_cell_tags = ["del_cell"]
c.preprocessors = ['TagRemovePreprocessor'] # Was previously: c.NotebookExporter.preprocessors
nb_body, resources = NotebookExporter(config=c).from_filename('notebook.ipynb')
nbformat.write(nbformat.from_dict(json.loads(nb_body)),'stripped_notebook.ipynb',4)
这篇关于使用nbconvert作为库运行预处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!