Nbconvert: Standard way to remove all code cells from PDF export

Created on 5 Nov 2015  路  22Comments  路  Source: jupyter/nbconvert

What is the standard way of removing all code cells from the exported PDF? I searched for it, and found many different "tplx" templates, for various versions. I tried some of them but they didn't seem to work. Is there a standard way of doing this?

Thanks!

Most helpful comment

The hide_code extension should really be baked in the main juptyer notebook repository. I think this can be perceived as a basic functionality in many settings.

The person performing the analysis normally needs to create a report - with hide_code a report with Markdown cells, plot cells & table outputs is comfortably created and can be handed over to co-workers/collaborators/customers who do not care about the coding part. For those people the code is really disturbing, and handing over a document which is half code half report does seem half-baked and they could think that the analyst is too lazy to create a tidy report.

All 22 comments

No. We are working on ability for external libraries to provides templates that woudl do such things.

This custom template has what you want:
https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/templates/printviewlatex.tplx
Look at the "Input" part.

If your codecell contains the metadate cell.metadata.hide_input=True, it will be hidden in the generated PDF. There are also some extensions in this repository that allow setting this metadata (hide_input, hide_input_all, runtools).

@juhasch Thanks a lot for pointing me to that repo! Most of it is working great.

I added two PRs and an issue on this point:
https://github.com/ipython-contrib/IPython-notebook-extensions/issues/399

I found this googling around how to suppress output of individual cells when exporting to PDF (like you can in R markdown documents by using code chunk echo=False).

It took me a little while to understand the above so I'll spell out what I did more explicitly.

Versions I'm using:
$ jupyter notebook --version
4.1.0
$ jupyter nbconvert --version
4.2.0

  1. Download the ipython notebook extension templates by following install instructions on Github: pip install https://github.com/ipython-contrib/IPython-notebook-extensions/tarball/master
  2. run jupyter notebook
  3. go to localhost:8888/nbextensions (or whatever port you started on) and activate Printview
  4. go back to localhost:8888/tree, create a new notebook and go into it
  5. create a code cell with some code in it that produces output e.g. print("You can see me") #but not me
  6. go to View > Cell Toolbar > Edit Metadata
  7. click the Edit Metadata button now showing to the top right of the cell
  8. add 'hide_input':True to the json e.g. mine looked like { "collapsed": false, "hide_input": true, "trusted": true } after
  9. save notebook
  10. go back to the terminal and execute jupyter nbconvert --to pdf --template printviewlatex.tplx notebookname.ipynb (if your notebook is called notebookname.ipynb.ipynb)

You should now have a document called notebookname.pdf in the directory. Hopefully it should have just the text You can see me in it...fingers crossed.

Hi @kungfujam , I installed the ipython_notebook_extensions tarball, I can locate it in /usr/local/lib/python2.7/dist-packages under the directory jupyter_contrib_nbextentions/templates, but when I try to execute the command jupyter nbconvert --to pdf --template printviewlatex.tplx I get the following error: jinja2.exceptions.TemplateNotFound: printviewlatex.tplx. Do you know how can I solve this? Thanks a lot in advance

You need to set the template_path configuration for nbconvert to point to the template:
http://nbconvert.readthedocs.io/en/latest/config_options.html

Example for jupyter_nbconvert_configuration.py:

c = get_config()
c.Exporter.template_path = ['.']

Is there a way to do it for html?

Sure, use the nbextensions.tpl file. Documentation is here.

How to convert using python api?
I have found

converter = NbConvertApp()
converter.export_format = 'html'
converter.initialize()
converter.notebooks = ["report.ipynb"]
converter.convert_notebooks()

But i don`t know how to set temaplte using this class

I wouldn't use NbConvertApp, that's really there for the command line interface. I would do something like this:

from nbconvert import HTMLExporter
exporter = HTMLExporter(template_file='full.tpl')
output, resources = exporter.from_filename('report.ipynb')
# output is a string; you can write it to a file, save it to disk, etc.

That is what I wanted
thanks!!!

Any news on this functionality from nbconvert? In essence being able to suppress code but not necessarily output when converting a notebook to e.g. .pdf.

Never mind, just found the hide_code extension.

The hide_code extension should really be baked in the main juptyer notebook repository. I think this can be perceived as a basic functionality in many settings.

The person performing the analysis normally needs to create a report - with hide_code a report with Markdown cells, plot cells & table outputs is comfortably created and can be handed over to co-workers/collaborators/customers who do not care about the coding part. For those people the code is really disturbing, and handing over a document which is half code half report does seem half-baked and they could think that the analyst is too lazy to create a tidy report.

It could be useful to point to the new --no-input option of jupyter nbconvert. The option works with both PDF and HTML output. For instance:

echo 'In this notebook we compute $1+1=\dots$

```python
1 + 1
```
' | jupytext --to ipynb | jupyter nbconvert --stdin --execute --no-input --to pdf --output notebook.pdf

produces
image

Many more options (including TemplateExporter.exclude_input) are available, cf. the nbconvert documentation.

Thank you so much for the pointer, @mwouts - I haven't stumbled across this option yet!

Thanks @mwouts! That worked for me just wonderfully.

@mwouts this is very useful !

@mwouts thanks for the help!

@mwouts Great feature! It seems like it only works when exporting to HTML. When exporting to PDF with --no-input the code cells are still included.

Is this a bug or expected behavior? Adding this feature to the PDF exporter would make it easy to generate professional looking reports with jupyter

Thanks everyone!

@maxhartshorn , the above example still works, with no code cell in the PDF, at least here on Ubuntu 20.04 LTS with textlive-xetex and the jupytext-dev conda environment. What is the outcome of jupyter nbconvert --version for you? (mine is 5.6.1)

@mwouts I'm on 5.6.1 as well...on Windows 10. I'm having trouble getting the above sample to run in my Anaconda prompt. However when I take an existing .ipynb file and save out to a pdf with the --no-input flag I still get the code cells. My command is as follows:

`[FILENAME].ipynb --execute --no-input --to pdf --output [FILENAME].pdf

Well, just to be sure I gave it a try on Windows 10. I installed MikTex, and then created a simple environment with

conda env create --file environment.yml

based on this environment.yml file:

name: mini-env
channels:
  - default
  - conda-forge
dependencies:
  - nbconvert
  - jupyter_client
  - ipykernel
  - jupytext

Then, after running conda activate mini-env, I was able to run the nbconvert --no-input example, and I did got the same PDF than on Linux, without the input cells. So it is possible to get it working on Windows as well... @maxhartshorn , would you like to try the command in the mini-env environment?

Was this page helpful?
0 / 5 - 0 ratings