Suppress code in NBConvert? IPython

2019-01-16 16:59发布

I have figured out how to suppress large code blocks from showing up in final NB convert (PDF) output.

By putting the LaTex command in a "raw cell before the code I don't want to have in the final output

\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? Sorry for a second issue but I'm not sure I want to ask this one separately for some strange reason.

3条回答
等我变得足够好
2楼-- · 2019-01-16 17:30

Corrections to the answer above:

1) Correct document class name in the latex file above

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

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

3) Call the converter as

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

查看更多
叼着烟拽天下
3楼-- · 2019-01-16 17:45

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 *))

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. Hence the example from above should look like

((*- extends 'article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))
查看更多
冷血范
4楼-- · 2019-01-16 17:56

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. Use TorokLev's latex template (say pdf_nocode.tplx) and then create a python file (say pdf_nocode.py) and paste the following:

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:

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
查看更多
登录 后发表回答