J Grif
J Grif

Reputation: 1093

ipython notebook nbconvert - how to remove red 'out[N]' text in top left hand corner of cell output?

I am using nbconvert to produce something as close as possible to a polished journal article.

I have successfully hidden input code using a custom nbconvert template. The doc is now looking very nice.

But I don't know how to suppress the bright red 'out[x]' statement in the top left corner of the output cells. Anyone know of any settings or hacks that are able to remove this also ?

Thanks,

John

Upvotes: 8

Views: 6180

Answers (3)

Swier
Swier

Reputation: 4196

You can use IPython.display.display to display the values. This prevents the cell from having output. In addition, with the %%capture magic command you can prevent any remaining output (warnings, errors, etc.) from being rendered.

And for the exporter, you can pass exclude_input=True to hide all input cells.

An example:

%%capture --no-display

import logging
from IPython.display import display

logging.warning("this warning is captured and not displayed")
display(1+1)

will display 2 without any Out[1] or warnings.

You can then export the notebook without any of the input cells with:

nbconvert.export(exporter, 'notebook.ipynb', exclude_input=True)

Upvotes: 0

johnbaltis
johnbaltis

Reputation: 1588

%%HTML <style> div.prompt {display:none} </style>

This will hide both In and Out prompts

Note that this is only in your browser, the notebook itself isn't modified of course, and nbconvert will work just the same as before.

In case you want this in the nbconverted code as well, just put the <style>div.prompt {display:none}</style> in a Raw NBConvert cell.

Upvotes: 5

Jakob
Jakob

Reputation: 20831

Depending on the version of IPython you are using, there are more or less hackish ways to remove the Out[ ] prompts.

IPython 1.x

Assuming that you use the latex_article base, a custom template (sphinx_template.tplx) with removed input blocks could look like

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

To finally remove the prompt, you need to use the simple mode of the Sphinx style, hence use it like ipython nbconvert --to latex --SphinxTransformer.output_style=simple --template=sphinx_template.tplx test.ipynb

IPython Master

In IPython master additional cell styles have been added, see e.g. PR4112. How to use these styles is shown e.g. in example1 and examples2.

To sum up, here the template (bw_python.tplx) could look like (with inputs)

((= 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' *))

This is used without additional options, hence ipython nbconvert --to=latex --template=bw_python.tplx test.ipynb

Upvotes: 3

Related Questions