简体   繁体   English

抑制NBConvert中的代码? IPython的

[英]Suppress code in NBConvert? IPython

I have figured out how to suppress large code blocks from showing up in final NB convert (PDF) output. 我已经弄清楚如何抑制大型代码块出现在最终的NB转换(PDF)输出中。

By putting the LaTex command in a "raw cell before the code I don't want to have in the final output 通过将LaTex命令放在“我希望在最终输出中不需要的代码之前的原始单元格”中

\iffalse

Followed By this at the end In a raw cell 最后跟着这个原始单元格

\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? 但是当我需要显示数字等时,仍然留下一些丑陋的代码,而笔记本的基本目的是显示带有结果的代码,有时对于非技术用户我们只需要输出..任何想法?

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? 如果有人受到启发,有点相关..任何方式在markdown单元格中包含python变量,这样可以得到带有计算结果的动态文本? Sorry for a second issue but I'm not sure I want to ask this one separately for some strange reason. 对不起第二个问题,但我不确定是否因为某些奇怪的原因而单独询问这个问题。

To suppress the code cells (only input) a custom template can be used. 要禁止代码单元格(仅输入),可以使用自定义模板。 Similar as discussed in this question , a template eg latex_nocode.tplx has to be created (in the working directory) with the following content (for IPython 1.x) 此问题中讨论的类似,必须创建一个模板,例如latex_nocode.tplx (在工作目录中),其中包含以下内容(对于IPython 1.x)

((*- extends 'latex_article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))

use this template like 使用这个模板
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. 检查预定义的乳胶模板时,可以识别各个块(代码,标记,标题等),并可以设置相应的自定义模板以根据需要设置输出样式。

Edit 编辑

as user1248490 pointed out since IPython 2.0 the latex templates to be extended are called article.tplx , report.tplx or base.tplx . 正如user1248490指出,自IPython 2.0以来,要扩展的乳胶模板称为article.tplxreport.tplxbase.tplx Hence the example from above should look like 因此,上面的例子应该是这样的

((*- extends 'article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))

If you've stumbled here from IPython 3.2.0 on Windows on Anaconda (I can't see why this wouldn't work on unix systems or on a regular ipython install, but I have not tested those scenarios myself) - this method is what's worked for me. 如果你在Anaconda上的Windows上从IPython 3.2.0发现这里(我不明白为什么这对unix系统或常规的ipython安装不起作用,但我自己没有测试过这些场景) - 这个方法是什么对我有用。 Use TorokLev's latex template (say pdf_nocode.tplx) and then create a python file (say pdf_nocode.py) and paste the following: 使用TorokLev的乳胶模板(比如pdf_nocode.tplx),然后创建一个python文件(比如pdf_nocode.py)并粘贴以下内容:

c = get_config()

#Export all the notebooks in the current directory to the sphinx_howto format.
c.NbConvertApp.notebooks = ['*.ipynb']
c.NbConvertApp.export_format = 'pdf'
c.TemplateExporter.template_path = ['.', r"C:\your\path\to\tplx\folder"]
c.Exporter.template_file = 'pdf_nocode'

Finally, your command will be: 最后,您的命令将是:

ipython nbconvert --to=pdf --config C:\your\path\to\pdf_nocode.py

This will also generate the images as support files, if you want to suppress that output (I use mostly plots so this may not work for everyone), you can either modify 这也会生成图像作为支持文件,如果你想抑制那个输出(我主要使用的是图,所以这对每个人都不适用),你可以修改

site-packages\IPython\nbconvert\exporters\pdf.py

by adding this code: 通过添加此代码:

...
def from_notebook_node(self, nb, resources=None, **kw):
...
        # convert output extension to pdf
        # the writer above required it to be tex
        resources['output_extension'] = '.pdf'

        #The following suppresses the support files, so you may
        #end up removing other useful files you wanted to include
        del resources['outputs']
...

or by subclassing PDFExporter from pdf.py and adding your exporter in exporter_map to: 或者通过从pdf.py继承PDFExporter并将exporter_map中的导出器添加到:

site-packages\IPython\nbconvert\exporters\export.py

If you subclass it does have a side benefit of you being able to add your own entry to the notebook menu in: 如果你是子类,它确实有一个附带的好处,你可以在下面的笔记本菜单中添加自己的条目:

site-packages\IPython\html\static\notebook\js\menubar.js

Corrections to the answer above: 以上答案的更正:

1) Correct document class name in the latex file above 1)在上面的latex文件中更正文档类名

You should change the line 你应该改变这条线

((*- extends 'article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))

2) Add local directory to nbconvert config as 2)将本地目录添加到nbconvert config as

sudo gedit /usr/local/lib/python2.7/dist-packages/jupyter_core/tests/dotipython/profile_default/ipython_nbconvert_config.py

add a line c.TemplateExporter.template_path = ['.'] to the file 在文件中添加一行c.TemplateExporter.template_path = ['.']

3) Call the converter as 3)将转换器称为

ipython nbconvert --to=pdf --template=latex_nocode.tplx tested_notebook.ipynb

or if you want to execute all cells for a report for example: 或者如果要执行报告的所有单元格,例如:

ipython nbconvert --to=pdf --template=latex_nocode.tplx --ExecutePreprocessor.enabled=True tested_notebook.ipynb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM