抑制 NBConvert 中的代码?蟒蛇 [英] Suppress code in NBConvert? IPython
问题描述
我已经找到了如何抑制大代码块出现在最终 NB 转换 (PDF) 输出中的方法.
I have figured out how to suppress large code blocks from showing up in final NB convert (PDF) output.
通过将 LaTex 命令放在我不想在最终输出中包含的代码之前的原始单元格中
By putting the LaTex command in a "raw cell before the code I don't want to have in the final output
iffalse
最后在原始单元格中
fi
但是当我需要显示数字等时,这仍然给我留下了一些丑陋的代码,而笔记本的基本目的是显示带有结果的代码,有时对于非技术受众,我们只需要输出..任何想法?
But That still leaves me with some ugly code when I need to show figures and the like and while the base purpose of the notebook is to show code with results, sometimes for a non tech audience we only need the output.. Any Ideas?
有点相关,如果有人受到启发..有什么方法可以在降价单元格中包含python变量,这样就可以获得带有计算结果的动态文本?抱歉,有第二个问题,但我不确定是否出于某种奇怪的原因要单独问这个问题.
Somewhat related if anyone is inspired.. any way to include python variables in the markdown cells so one could have dynamic text with calculated result? Sorry for a second issue but I'm not sure I want to ask this one separately for some strange reason.
推荐答案
要抑制代码单元格(仅输入),可以使用自定义模板.与这个问题中讨论的类似,模板例如latex_nocode.tplx 必须使用以下内容(对于 IPython 1.x)创建(在工作目录中)
To suppress the code cells (only input) a custom template can be used. Similar as discussed in this question, a template e.g. latex_nocode.tplx has to be created (in the working directory) with the following content (for IPython 1.x)
((*- extends 'latex_article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))
像这样使用这个模板ipython nbconvert --to=latex --template=latex_nocode.tplx --post=pdf file.ipynb
也许我应该补充一点,这样输入块就被一个空白块替换了(实际上是输入单元格被禁用的乳胶注释).
在检查预定义的乳胶模板时,可以识别单个块(代码、降价、标题等),并且可以设置相应的自定义模板以根据需要设置输出样式.
Maybe I should add that this way the input block is simply replaced by a blank block (actually a latex comment that input cells are disabled).
When checking the predefined latex templates, the individual blocks (code, markdown, heading, etc) can be identified and a respective custom templates can be set-up to style the output as desired.
正如 user1248490 指出的,自 IPython 2.0 开始,要扩展的乳胶模板被称为article.tplx
、report.tplx
或 base.tplx
.因此上面的例子应该看起来像
as user1248490 pointed out since IPython 2.0 the latex templates to be extended are called
article.tplx
, report.tplx
or base.tplx
. Hence the example from above should look like
((*- extends 'article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))
这篇关于抑制 NBConvert 中的代码?蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!