Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress code in NBConvert? IPython

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.

like image 665
dartdog Avatar asked Oct 22 '13 17:10

dartdog


People also ask

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

How do you collapse code cells in Jupyter Notebook?

When you create a new notebook, ideally you should see small triangles next to your code indicating that you can now collapse them as required. You can hide/show the contents of the cell using the triangle and the blue button as shown in the image.

How do you remove a tag from a Jupyter Notebook?

in Jupyter - Home-running- stop your file, after that go to the explorer- click right bottom on your_file. ipynb open with notebook(just like . txt) - scroll down, find raw with "celltoolbar": "None", and delete this and save file, after run file in jupyter.


2 Answers

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 *))
like image 115
Jakob Avatar answered Sep 22 '22 22:09

Jakob


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
like image 37
dworvos Avatar answered Sep 20 '22 22:09

dworvos