我正在使用nbconvert生成尽可能接近抛光期刊文章的内容。
我使用自定义nbconvert模板成功隐藏了输入代码。该文档现在看起来非常好。
但我不知道如何抑制输出单元格左上角的亮红色'out [x]'语句。有人知道任何能够删除它的设置或黑客吗?
谢谢,
约翰
答案 0 :(得分:4)
%%HTML
<style>
div.prompt {display:none}
</style>
这将隐藏In和Out提示
请注意,这只是在您的浏览器中,当然,笔记本本身并未修改,而nbconvert
的工作方式与之前相同。
如果您想在nbconvert
ed代码中使用此代码,只需将<style>div.prompt {display:none}</style>
放入Raw NBConvert单元格中。
答案 1 :(得分:3)
根据您使用的IPython版本,有一些或多或少的hackish方法可以删除Out []提示。
假设您使用latex_article
基础,带有删除输入块的自定义模板(sphinx_template.tplx)可能看起来像
((* extends 'latex_article.tplx' *))
((* block input *))
((* endblock input *))
((* block output_group *))
% Add remainer of the document contents below.
((* for output in cell.outputs *))
((( render_output(output) )))
((* endfor *))
((* endblock *))
要最终删除提示,您需要使用Sphinx样式的simple
模式,因此请使用它
ipython nbconvert --to latex --SphinxTransformer.output_style=simple --template=sphinx_template.tplx test.ipynb
在IPython master中添加了其他单元格样式,请参阅例如PR4112。 如何使用这些样式如图所示。在example1和examples2。
总而言之,这里的模板(bw_python.tplx)看起来像(带输入)
((= This line selects the cell style. =))
((* set cell_style = 'style_bw_python.tplx' *))
((= This line inherits from the built in template that you want to use. =))
((* extends 'latex_article.tplx' *))
因此无需其他选项即可使用
ipython nbconvert --to=latex --template=bw_python.tplx test.ipynb
答案 2 :(得分:0)
您可以使用 IPython.display.display
来显示值。这可以防止单元格有输出。此外,使用 %%capture
魔术命令,您可以防止呈现任何剩余的输出(警告、错误等)。
对于导出器,您可以传递 exclude_input=True
以隐藏所有输入单元格。
示例:
%%capture --no-display
import logging
from IPython.display import display
logging.warning("this warning is captured and not displayed")
display(1+1)
将显示 2
没有任何 Out[1]
或警告。
然后您可以导出没有任何输入单元格的笔记本:
nbconvert.export(exporter, 'notebook.ipynb', exclude_input=True)